Page 1 of 1

fcntl in py 2.6

PostPosted: Fri May 01, 2015 12:13 am
by kw123
In py 2.7 the attached test.py program reads the stream of data generated by pgm.py(*) nicely, but NOT when used under py2.6 (in plugin or standalone program)

python program is started with
/usr/bin/python2.6 test.py or ... python2.7 test.py

test.py:
Code: Select all
import fcntl, os, subprocess, time
p = subprocess.Popen([pythonPath, '/xxx/yyy/pgm.py'], stdout = subprocess.PIPE, shell = False)
flags = fcntl.fcntl(p.stdout, fcntl.F_GETFL) # get current p.stdout flags
fcntl.fcntl(p.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
while True:
   try:
      time.sleep(1)
      print "hello"
      xx =  os.read(p.stdout.fileno(), 1024)
      if len(xx) > 5: print xx
   except OSError:
      time.sleep(5)

(*) pgm.py:
Code: Select all
import time
while true:
  print "abc"
  sys.stdout.flush()
  time.sleep(3)


any advice?

thx

Karl

Re: fcntl in py 2.6

PostPosted: Fri May 01, 2015 3:52 am
by autolog
Hi Karl,

I am not sure exactly what output you are expecting but in seems similar in python2.6 vs python2.7.

I had some difficulty exactly replicating your test. I had to make changes to pgm.py to get it working:
Code: Select all
import time
import sys
while True:
  print "abc"
  sys.stdout.flush()
  time.sleep(3)
i.e. add import sys and change true to True

Also when running test.pgm, it couldn't find pythonPath so I set it to '/usr/bin/python2.6' and '/usr/bin/python2.7' before each test as appropriate. I added in a print statement to print out the value of the pythonPath variable as a double check. :)

The output I got from a 2.6 test was:
$ python2.6 test.py
pythonPath used for test: /usr/bin/python2.6
hello
hello
hello
abc
abc

hello
hello
abc
abc

hello
hello
hello
abc
abc

hello
hello
hello
abc
abc

hello
hello
abc
abc

hello

and the output from a 2.7 test was:
$ python2.7 test.py
pythonPath used for test: /usr/bin/python2.7
hello
hello
hello
abc
abc

hello
hello
abc
abc

hello
hello
hello
abc
abc

hello
hello
abc
abc

hello
hello
hello
abc
abc

hello


Does this help at all?

Re: fcntl in py 2.6

PostPosted: Fri May 01, 2015 8:21 am
by kw123
I stripped down my real py program. :oops: .

and when I actually do it very simple it actually works. thanks for debugging, ...

now I have to step by step add back functionality.

Karl