chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] gnuplot subprocess: python->chicken


From: Bryan Vicknair
Subject: [Chicken-users] gnuplot subprocess: python->chicken
Date: Tue, 30 Apr 2013 10:41:14 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

Hello,

I'm currently porting some web apps from Python to Chicken, and really enjoying
the combination of minimalism and expressiveness of Chicken/Scheme.

I'm having trouble interacting with a gnuplot subprocess.  Here is an example
Python script that will generate an ugly graph @ /tmp/graph-py.png::

    from subprocess import Popen, PIPE

    cmd = """
    set terminal pngcairo size 460,344 font 'Verdana,10'
    set output '/tmp/graph-py.png'
    plot '-' using 1:2
    2012-09-23  0
    2012-09-30  24
    2012-10-07  63
    """

    def go():
        gnuplot = Popen("gnuplot", stdin=PIPE, stdout=PIPE, stderr=PIPE)
        out, err = gnuplot.communicate(cmd + "\nexit")
        if err:
            # This happens: "Warning: empty x range..." but graph is created.
            raise IOError(err)


Here is my attempt to do the same in Chicken::

    (use srfi-1 srfi-13 posix extras)

    (define cmd "
    set terminal pngcairo size 460,344 font 'Verdana,10'
    set output '/tmp/graph-scm.png'
    plot '-' using 1:2
    2012-09-23  0
    2012-09-30  24
    2012-10-07  63
    ")

    (define go
      (lambda ()
        (define-values (i o pid stderr) (process* "gnuplot"))
        (write-line cmd o)
        (write-line "exit" o)

        ; Hangs trying to read stderr.
        (let ((err (read-string #f stderr)))
          (if (> (string-length err) 0)
            (error err)
            (begin
              (close-input-port i)
              (close-output-port o)
              )))))


The Chicken example hangs when I try to read stderr.  If I don't attempt to
read the 'o' or 'stderr' ports in the Chicken example, a call to (go) returns,
and the graph is created, but the gnuplot process becomes a zombie.

How can I read the output/err ports without hanging, even if there is no data
on the ports?  Also, the docs say that after closing the input/output of a
subprocess, an implicit waitpid happens, so why is gnuplot becoming a zombie?


Bryan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]