unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* open-pipe* seems to buffer all of my writes when I don't want it.
@ 2021-01-15  4:56 Tim Meehan
  2021-01-15  9:16 ` tomas
  0 siblings, 1 reply; 3+ messages in thread
From: Tim Meehan @ 2021-01-15  4:56 UTC (permalink / raw)
  To: guile-user

I was trying to use Matplotlib to plot some data, through a call to
"open-pipe*" ... I'm trying to slowly rewrite some of my tools in Scheme.

When I run the program from the shell with "python -i plotter.py" I can
send it updates ... but when I run it through Guile, it seems to store up
all of my updates, then right before the program closes a window pops up
for a fraction of a second and then vanishes. Am I using "open-pipe*"
incorrectly?

#!/usr/bin/guile
!#

(use-modules (ice-9 popen)
             (ice-9 readline)
             (ice-9 textual-ports))

(define program (string-concatenate
    '("import scipy.special\n"
      "import matplotlib.pyplot as plt\n"
      "import numpy as np\n"

      "x  = np.linspace(0, 10)\n"
      "y0 = scipy.special.jv(0, x)\n"

      "def update_data():\n"
      "    print('updating')\n"
      "    noise = np.random.randn(y0.size)*0.1\n"
      "    line1.set_ydata(y0 + noise)\n"
      "    plt.pause(0.1)\n"

      "fig, ax = plt.subplots(1, 2)\n"
      "ax[0].plot(x, y0)\n"
      "ax[0].set_title('Original Data')\n"
      "ax[0].set_xlabel('X')\n"
      "ax[0].set_ylabel('Y')\n"

      "ax[1].plot(x, y0)\n"
      "line1, = ax[1].plot(x, y0, '.')\n"
      "ax[1].set_title('Noisy Data')\n"
      "ax[1].set_xlabel('X')\n"
      "ax[1].set_ylabel('Y')\n"
      "fig.show()\n"
      "plt.pause(0.1)\n")))


(define prompt "update? ('quit' to stop')")
(define stuff (open-pipe* OPEN_WRITE "python3"))
(setvbuf stuff 'none)

(put-string stuff program)
(force-output stuff)

(let loop ([command (readline prompt)])
    (unless (string-prefix? "quit" command)
        (display "updating\n")
        (put-string stuff "update_data()\n")
        (loop (readline prompt))))

(close-pipe stuff)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: open-pipe* seems to buffer all of my writes when I don't want it.
  2021-01-15  4:56 open-pipe* seems to buffer all of my writes when I don't want it Tim Meehan
@ 2021-01-15  9:16 ` tomas
  2021-01-16  2:39   ` Tim Meehan
  0 siblings, 1 reply; 3+ messages in thread
From: tomas @ 2021-01-15  9:16 UTC (permalink / raw)
  To: Tim Meehan; +Cc: guile-user

[-- Attachment #1: Type: text/plain, Size: 874 bytes --]

On Thu, Jan 14, 2021 at 10:56:42PM -0600, Tim Meehan wrote:
> I was trying to use Matplotlib to plot some data, through a call to
> "open-pipe*" ... I'm trying to slowly rewrite some of my tools in Scheme.
> 
> When I run the program from the shell with "python -i plotter.py" I can
> send it updates ... but when I run it through Guile, it seems to store up
> all of my updates [...]

Warning: untested. But perhaps you need to put a `force-output'...

[...]
> (put-string stuff program)
> (force-output stuff)
> 
> (let loop ([command (readline prompt)])
>     (unless (string-prefix? "quit" command)
>         (display "updating\n")
>         (put-string stuff "update_data()\n")

... around here?

>         (loop (readline prompt))))
> 
> (close-pipe stuff)

(I'm still surprised that setting the buffer to 'none didn't help).

Cheers
 - t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: open-pipe* seems to buffer all of my writes when I don't want it.
  2021-01-15  9:16 ` tomas
@ 2021-01-16  2:39   ` Tim Meehan
  0 siblings, 0 replies; 3+ messages in thread
From: Tim Meehan @ 2021-01-16  2:39 UTC (permalink / raw)
  To: tomas; +Cc: guile-user

I tried the force output right there. It still doesn’t pop up the window until you type “quit,” it then shows the window for a while (because of the Python pause calls).

Thanks for looking at it

> On Jan 15, 2021, at 03:16, tomas@tuxteam.de wrote:
> 
> On Thu, Jan 14, 2021 at 10:56:42PM -0600, Tim Meehan wrote:
>> I was trying to use Matplotlib to plot some data, through a call to
>> "open-pipe*" ... I'm trying to slowly rewrite some of my tools in Scheme.
>> 
>> When I run the program from the shell with "python -i plotter.py" I can
>> send it updates ... but when I run it through Guile, it seems to store up
>> all of my updates [...]
> 
> Warning: untested. But perhaps you need to put a `force-output'...
> 
> [...]
>> (put-string stuff program)
>> (force-output stuff)
>> 
>> (let loop ([command (readline prompt)])
>>    (unless (string-prefix? "quit" command)
>>        (display "updating\n")
>>        (put-string stuff "update_data()\n")
> 
> ... around here?
> 
>>        (loop (readline prompt))))
>> 
>> (close-pipe stuff)
> 
> (I'm still surprised that setting the buffer to 'none didn't help).
> 
> Cheers
> - t



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-01-16  2:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-15  4:56 open-pipe* seems to buffer all of my writes when I don't want it Tim Meehan
2021-01-15  9:16 ` tomas
2021-01-16  2:39   ` Tim Meehan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).