You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python is pretty unfriendly when interrupting threads from outside of threads. The problem is that the interruption may happen in a finalization block, which releases resources, e.g. releasing global locks. If this happen, the thread may become completely broken. So, in most cases such a function is a ticking bomb. E.g. check this code snippet:
finalization_completed = False
try:
with timeout(2):
try:
print("main: started", flush=True)
time.sleep(1)
print("main: completed", flush=True)
finally:
print("finalization: started", flush=True)
time.sleep(2)
print("starting a very important finalization, e.g. a lock release", flush=True)
finalization_completed = True
finally:
print(f"finalization_completed={finalization_completed}", flush=True)
output:
main: started
main: completed
finalization: started
finalization_completed=False
Traceback (most recent call last):
File "<stdin>", line 9, in <module>
File "<stdin>", line 4, in _handle_timeout
TimeoutException: Timeout after 2 seconds
The text was updated successfully, but these errors were encountered:
Python is pretty unfriendly when interrupting threads from outside of threads. The problem is that the interruption may happen in a finalization block, which releases resources, e.g. releasing global locks. If this happen, the thread may become completely broken. So, in most cases such a function is a ticking bomb. E.g. check this code snippet:
output:
The text was updated successfully, but these errors were encountered: