Skip to content

Commit a84e1b5

Browse files
committed
avoid pristine_loop/loop.start in loop_in_thread
1 parent 8d5643c commit a84e1b5

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

distributed/utils_test.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,29 @@ def start():
170170

171171
@pytest.fixture
172172
def loop_in_thread(cleanup):
173-
with pristine_loop() as loop:
174-
thread = threading.Thread(target=loop.start, name="test IOLoop")
175-
thread.daemon = True
176-
thread.start()
177-
loop_started = threading.Event()
178-
loop.add_callback(loop_started.set)
179-
loop_started.wait()
180-
yield loop
181-
loop.add_callback(loop.stop)
182-
thread.join(timeout=5)
173+
loop_started = concurrent.futures.Future()
174+
with concurrent.futures.ThreadPoolExecutor(
175+
1, thread_name_prefix="test IOLoop"
176+
) as tpe:
177+
178+
async def run():
179+
io_loop = IOLoop.current()
180+
stop_event = asyncio.Event()
181+
loop_started.set_result((io_loop, stop_event))
182+
await stop_event.wait()
183+
184+
ran = tpe.submit(_run_and_close_tornado, run)
185+
for f in concurrent.futures.as_completed((loop_started, ran)):
186+
if f is loop_started:
187+
io_loop, stop_event = loop_started.result()
188+
try:
189+
yield io_loop
190+
finally:
191+
io_loop.add_callback(stop_event.set)
192+
193+
elif f is ran:
194+
ran.result()
195+
return
183196

184197

185198
@pytest.fixture

0 commit comments

Comments
 (0)