Python anonymous pipes set timeouts … here is a solution to the problem.
Python anonymous pipes set timeouts
I don’t think it’s possible; But is there a way to set a read timeout on an anonymous pipe in Python/C on Linux?
IS THERE A BETTER OPTION THAN SETTING UP AND CAPTURING SIGALRM?
>>> import os
>>> output, input = os.pipe()
>>> outputfd = os.fdopen(output, 'r')
>>> dir(outputfd)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__ repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']
>>>
(There is no settimeout()
method
.)
Solution
You should try using the select
module, which allows you to provide a timeout. Add the file object to the selection set and check the returned object to see if it has changed:
r, w, x = select.select([output], [], [], timeout)
Then check r to see if the object is readable. This can be extended to as many objects as you want to monitor. If the object is in R, a read: output.read()
is performed.
Also, you may want to use os.read
instead of fdopen because it is not affected by Python file buffering.