I have a feeling its not possible; but is there a way to set a read timeout on an anonymous pipe in Python / C on Linux?
Are there better options than setting and trapping a 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']
>>>
(no settimeout() method)
You should try using the
selectmodule, which does allow you to provide a timeout. Add the file object to the select set, and then examine the return object to see if it’s changed:Then examine r to see if the object is readable. This can be extended to as many objects you want to monitor. If the object is in r, then do a read:
output.read().Also, you may wish to use
os.read, rather than fdopen, as it will not be subject to the whims of Python’s file buffering.