Skip to content

Commit 3d2a604

Browse files
committed
Addresses #1958: Move code that handles job into its own method
1 parent dc0e9bc commit 3d2a604

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

trio/_core/_thread_cache.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,22 @@ def __init__(self, thread_cache):
5656
thread.name = f"Trio worker thread {next(name_counter)}"
5757
thread.start()
5858

59+
def _handle_job(self):
60+
fn, deliver = self._job
61+
self._job = None
62+
result = outcome.capture(fn)
63+
# Tell the cache that we're available to be assigned a new
64+
# job. We do this *before* calling 'deliver', so that if
65+
# 'deliver' triggers a new job, it can be assigned to us
66+
# instead of spawning a new thread.
67+
self._thread_cache._idle_workers[self] = None
68+
deliver(result)
69+
5970
def _work(self):
6071
while True:
6172
if self._worker_lock.acquire(timeout=IDLE_TIMEOUT):
6273
# 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
74+
self._handle_job()
7475
else:
7576
# Timeout acquiring lock, so we can probably exit. But,
7677
# there's a race condition: we might be assigned a job *just*

0 commit comments

Comments
 (0)