When using trio.sleep()
or anyio.sleep()
with very long intervals (greater than 24 hours), the intent is usually to wait
indefinitely or for an extremely long period. Both libraries provide dedicated methods specifically designed for this purpose:
trio.sleep_forever()
and anyio.sleep_forever()
.
Using explicit sleep durations greater than 24 hours has several drawbacks:
- It obscures the developer’s intent. A very large number like
86400 * 365
doesn’t clearly communicate that the code intends to wait
indefinitely.
- It makes code less maintainable, as other developers need to calculate what the large numbers actually represent.
In summary, using sleep_forever()
is preferable when the intent is to sleep indefinitely as it clearly conveys this purpose, avoiding
maintainability issues caused by using arbitrarily large sleep durations.