A few threads are started in my code and I need at the end of the script to sleep indefinitely, without this sleep being a major hit on the performance1.
One possibility can be to loop indefinitely with a short sleep:
while True:
time.sleep(1)
or sleep for a long time
time.sleep(4000000)
or
import signal
signal.pause()
But:
I did not manage to find the largest time sleep would accept (
sys.maxint
is too large)signal.pause()
is implemented in Unix onlyand the first "sleep loop" does not look clean to me (why 1 second and not 10, or 0.1?)
Is there a clean, pythonic way to sleep indefinitely?
1 I do not control the threads directly, otherwise I would have gone for threading.Thread.join()
as the threads themselves will not end.
join()
the threads or the thread pool? – Bustamanteclient.loop_forever()
as an example. – Errhinethreading.enumerate
gives you the list of all running threads including the main one, so you could wait (join
) them. – Rappingwhile True: i+= 1
will incrementi
millions of times per second, callingtime.sleep(1)
introduces a relatively tiny amount of overhead, and sleeping longer as your granularity allows introduces even less. – Ankylosaur