@@ -56,21 +56,24 @@ def __init__(self, thread_cache):
56
56
thread .name = f"Trio worker thread { next (name_counter )} "
57
57
thread .start ()
58
58
59
+ def _handle_job (self ):
60
+ # Handle job in a separate method to ensure user-created
61
+ # objects are cleaned up in a consistent manner.
62
+ fn , deliver = self ._job
63
+ self ._job = None
64
+ result = outcome .capture (fn )
65
+ # Tell the cache that we're available to be assigned a new
66
+ # job. We do this *before* calling 'deliver', so that if
67
+ # 'deliver' triggers a new job, it can be assigned to us
68
+ # instead of spawning a new thread.
69
+ self ._thread_cache ._idle_workers [self ] = None
70
+ deliver (result )
71
+
59
72
def _work (self ):
60
73
while True :
61
74
if self ._worker_lock .acquire (timeout = IDLE_TIMEOUT ):
62
75
# We got a job
63
- fn , deliver = self ._job
64
- self ._job = None
65
- result = outcome .capture (fn )
66
- # Tell the cache that we're available to be assigned a new
67
- # job. We do this *before* calling 'deliver', so that if
68
- # 'deliver' triggers a new job, it can be assigned to us
69
- # instead of spawning a new thread.
70
- self ._thread_cache ._idle_workers [self ] = None
71
- deliver (result )
72
- del fn
73
- del deliver
76
+ self ._handle_job ()
74
77
else :
75
78
# Timeout acquiring lock, so we can probably exit. But,
76
79
# there's a race condition: we might be assigned a job *just*
0 commit comments