-
Notifications
You must be signed in to change notification settings - Fork 0
timeout of a function in python
M. Bai edited this page Apr 11, 2022
·
2 revisions
https://stackoverflow.com/questions/1191374/using-module-subprocess-with-timeout
threading.Thread.join(time)
https://stackoverflow.com/questions/492519/timeout-on-a-function-call
https://stackoverflow.com/a/14924210
import multiprocessing
import time
# bar
def bar():
for i in range(100):
print "Tick"
time.sleep(1)
if __name__ == '__main__':
# Start bar as a process
p = multiprocessing.Process(target=bar)
p.start()
# Wait for 10 seconds or until process finishes
p.join(10)
# If thread is still active
if p.is_alive():
print "running... let's kill it..."
# Terminate - may not work if process is stuck for good
p.terminate()
# OR Kill - will work for sure, no chance for process to finish nicely however
# p.kill()
p.join()
test page footer