Alternative to signals in threads
saatomic at keemail.me
saatomic at keemail.me
Wed Mar 8 03:36:53 EST 2017
I've been unsuccessfully looking for an alternative for signals, that works in threads.
After updating a script of mine from being single-threaded to being multi-threaded, I realised that signals do not work in threads.
I've used signals to handle blocking operations that possibly take forever like:
signal.signal(signal.SIGALRM, wait_timeout)
# wait_timeout simply does: raise Exception("timeout")
signal.setitimer(signal.ITIMER_REAL,5)
# So a timer of 5 seconds start and I run my operation
try:
p = Popen(["tool", "--param"], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
for line in p.stdout:
if "STRING" in str(line):
signal.setitimer(signal.ITIMER_REAL,0)
p.terminate()
print("Success")
except:
p.terminate()
print("Timeout")
Now this works great with a single thread, but doesn't work at all with multiple threads.
A more throughout example can be found here: https://gist.github.com/saatomic/841ddf9c5142a7c75b03daececa8eb17
What can I use instead of signals in this case?
Thanks and kind regards,
SaAtomic
More information about the Python-list
mailing list