0

I am trying to read and print initial screen of gnuplot via subprocess module:

G N U P L O T
Version 4.6 patchlevel 4    last modified 2013-10-02 
Build System: Linux x86_64
Copyright (C) 1986-1993, 1998, 2004, 2007-2013
Thomas Williams, Colin Kelley and many others
gnuplot home:     http://www.gnuplot.info
faq, bugs, etc:   type "help FAQ"
immediate help:   type "help"  (plot window: hit 'h')
Terminal type set to 'wxt'

This is my code:

from subprocess 
import PIPE, Popen
import fcntl, os
class Gnuplot:
def __init__(self, debug=True):
    self.debug = debug
    if self.debug:
        print 'Initializing ...\n' 
    # start process    
    self.proc = subprocess.Popen(['gnuplot'],stdin=PIPE,stdout=PIPE,stderr=PIPE)  
    # set stderr as nonblocking so that we can skip when there is nothing
    fcntl.fcntl(self.proc.stderr.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
#a = self.proc.communicate()
    fcntl.fcntl(self.proc.stderr.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
    cout = self.proc.communicate()
    if self.debug:
        print 'Done!\n'
    print cout
g= Gnuplot()

I don't know where my fault is. How can I fix this?

shruti1810
  • 3,920
  • 2
  • 16
  • 28
user3794176
  • 75
  • 1
  • 9

1 Answers1

0

This works for me on python 2 and 3, linux and windows (with something else than gnuplot) :

import subprocess
import fcntl
import os
import select


proc = subprocess.Popen(['gnuplot'],
                    stderr=subprocess.PIPE,
                    close_fds=True,
                    universal_newlines=True)
fcntl.fcntl(
    proc.stderr.fileno(),
    fcntl.F_SETFL,
    fcntl.fcntl(proc.stderr.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK)

status = select.select([proc.stderr.fileno()], [], [])[0]
if status:
    out = proc.stderr.read()
print(out)
proc.kill()
Ludovic Viaud
  • 202
  • 1
  • 5