| 1 |
import asyncio |
| 2 |
import threading |
| 3 |
import unittest |
| 4 |
from threading import Thread |
| 5 |
from unittest import TestCase |
| 6 |
import weakref |
| 7 |
from test import support |
| 8 |
from test.support import threading_helper |
| 9 |
|
| 10 |
threading_helper.requires_working_threading(module=True) |
| 11 |
|
| 12 |
|
| 13 |
class MyException(Exception): |
| 14 |
pass |
| 15 |
|
| 16 |
|
| 17 |
def tearDownModule(): |
| 18 |
asyncio.events._set_event_loop_policy(None) |
| 19 |
|
| 20 |
|
| 21 |
class TestFreeThreading: |
| 22 |
def test_all_tasks_race(self) -> None: |
| 23 |
async def main(): |
| 24 |
loop = asyncio.get_running_loop() |
| 25 |
future = loop.create_future() |
| 26 |
|
| 27 |
async def coro(): |
| 28 |
await future |
| 29 |
|
| 30 |
tasks = set() |
| 31 |
|
| 32 |
async with asyncio.TaskGroup() as tg: |
| 33 |
for _ in range(100): |
| 34 |
tasks.add(tg.create_task(coro())) |
| 35 |
|
| 36 |
all_tasks = asyncio.all_tasks(loop) |
| 37 |
self.assertEqual(len(all_tasks), 101) |
| 38 |
|
| 39 |
for task in all_tasks: |
| 40 |
self.assertEqual(task.get_loop(), loop) |
| 41 |
self.assertFalse(task.done()) |
| 42 |
|
| 43 |
current = asyncio.current_task() |
| 44 |
self.assertEqual(current.get_loop(), loop) |
| 45 |
self.assertSetEqual(all_tasks, tasks | {current}) |
| 46 |
future.set_result(None) |
| 47 |
|
| 48 |
def runner(): |
| 49 |
with asyncio.Runner() as runner: |
| 50 |
loop = runner.get_loop() |
| 51 |
loop.set_task_factory(self.factory) |
| 52 |
runner.run(main()) |
| 53 |
|
| 54 |
threads = [] |
| 55 |
|
| 56 |
for _ in range(10): |
| 57 |
thread = Thread(target=runner) |
| 58 |
threads.append(thread) |
| 59 |
|
| 60 |
with threading_helper.start_threads(threads): |
| 61 |
pass |
| 62 |
|
| 63 |
def test_all_tasks_different_thread(self) -> None: |
| 64 |
loop = None |
| 65 |
started = threading.Event() |
| 66 |
done = threading.Event() # used for main task not finishing early |
| 67 |
async def coro(): |
| 68 |
await asyncio.Future() |
| 69 |
|
| 70 |
lock = threading.Lock() |
| 71 |
tasks = set() |
| 72 |
|
| 73 |
async def main(): |
| 74 |
nonlocal tasks, loop |
| 75 |
loop = asyncio.get_running_loop() |
| 76 |
started.set() |
| 77 |
for i in range(1000): |
| 78 |
with lock: |
| 79 |
asyncio.create_task(coro()) |
| 80 |
tasks = asyncio.all_tasks(loop) |
| 81 |
done.wait() |
| 82 |
|
| 83 |
runner = threading.Thread(target=lambda: asyncio.run(main())) |
| 84 |
|
| 85 |
def check(): |
| 86 |
started.wait() |
| 87 |
with lock: |
| 88 |
self.assertSetEqual(tasks & asyncio.all_tasks(loop), tasks) |
| 89 |
|
| 90 |
threads = [threading.Thread(target=check) for _ in range(10)] |
| 91 |
runner.start() |
| 92 |
|
| 93 |
with threading_helper.start_threads(threads): |
| 94 |
pass |
| 95 |
|
| 96 |
done.set() |
| 97 |
runner.join() |
| 98 |
|
| 99 |
def test_task_different_thread_finalized(self) -> None: |
| 100 |
task = None |
| 101 |
async def func(): |
| 102 |
nonlocal task |
| 103 |
task = asyncio.current_task() |
| 104 |
def runner(): |
| 105 |
with asyncio.Runner() as runner: |
| 106 |
loop = runner.get_loop() |
| 107 |
loop.set_task_factory(self.factory) |
| 108 |
runner.run(func()) |
| 109 |
thread = Thread(target=runner) |
| 110 |
thread.start() |
| 111 |
thread.join() |
| 112 |
wr = weakref.ref(task) |
| 113 |
del thread |
| 114 |
del task |
| 115 |
# task finalization in different thread shouldn't crash |
| 116 |
support.gc_collect() |
| 117 |
self.assertIsNone(wr()) |
| 118 |
|
| 119 |
def test_run_coroutine_threadsafe(self) -> None: |
| 120 |
results = [] |
| 121 |
|
| 122 |
def in_thread(loop: asyncio.AbstractEventLoop): |
| 123 |
coro = asyncio.sleep(0.1, result=42) |
| 124 |
fut = asyncio.run_coroutine_threadsafe(coro, loop) |
| 125 |
result = fut.result() |
| 126 |
self.assertEqual(result, 42) |
| 127 |
results.append(result) |
| 128 |
|
| 129 |
async def main(): |
| 130 |
loop = asyncio.get_running_loop() |
| 131 |
async with asyncio.TaskGroup() as tg: |
| 132 |
for _ in range(10): |
| 133 |
tg.create_task(asyncio.to_thread(in_thread, loop)) |
| 134 |
self.assertEqual(results, [42] * 10) |
| 135 |
|
| 136 |
with asyncio.Runner() as r: |
| 137 |
loop = r.get_loop() |
| 138 |
loop.set_task_factory(self.factory) |
| 139 |
r.run(main()) |
| 140 |
|
| 141 |
def test_run_coroutine_threadsafe_exception(self) -> None: |
| 142 |
async def coro(): |
| 143 |
await asyncio.sleep(0) |
| 144 |
raise MyException("test") |
| 145 |
|
| 146 |
def in_thread(loop: asyncio.AbstractEventLoop): |
| 147 |
fut = asyncio.run_coroutine_threadsafe(coro(), loop) |
| 148 |
return fut.result() |
| 149 |
|
| 150 |
async def main(): |
| 151 |
loop = asyncio.get_running_loop() |
| 152 |
tasks = [] |
| 153 |
for _ in range(10): |
| 154 |
task = loop.create_task(asyncio.to_thread(in_thread, loop)) |
| 155 |
tasks.append(task) |
| 156 |
results = await asyncio.gather(*tasks, return_exceptions=True) |
| 157 |
|
| 158 |
self.assertEqual(len(results), 10) |
| 159 |
for result in results: |
| 160 |
self.assertIsInstance(result, MyException) |
| 161 |
self.assertEqual(str(result), "test") |
| 162 |
|
| 163 |
with asyncio.Runner() as r: |
| 164 |
loop = r.get_loop() |
| 165 |
loop.set_task_factory(self.factory) |
| 166 |
r.run(main()) |
| 167 |
|
| 168 |
|
| 169 |
class TestPyFreeThreading(TestFreeThreading, TestCase): |
| 170 |
|
| 171 |
def setUp(self): |
| 172 |
self._old_current_task = asyncio.current_task |
| 173 |
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._py_current_task |
| 174 |
self._old_all_tasks = asyncio.all_tasks |
| 175 |
asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._py_all_tasks |
| 176 |
self._old_Task = asyncio.Task |
| 177 |
asyncio.Task = asyncio.tasks.Task = asyncio.tasks._PyTask |
| 178 |
self._old_Future = asyncio.Future |
| 179 |
asyncio.Future = asyncio.futures.Future = asyncio.futures._PyFuture |
| 180 |
return super().setUp() |
| 181 |
|
| 182 |
def tearDown(self): |
| 183 |
asyncio.current_task = asyncio.tasks.current_task = self._old_current_task |
| 184 |
asyncio.all_tasks = asyncio.tasks.all_tasks = self._old_all_tasks |
| 185 |
asyncio.Task = asyncio.tasks.Task = self._old_Task |
| 186 |
asyncio.Future = asyncio.tasks.Future = self._old_Future |
| 187 |
return super().tearDown() |
| 188 |
|
| 189 |
def factory(self, loop, coro, **kwargs): |
| 190 |
return asyncio.tasks._PyTask(coro, loop=loop, **kwargs) |
| 191 |
|
| 192 |
|
| 193 |
@unittest.skipUnless(hasattr(asyncio.tasks, "_c_all_tasks"), "requires _asyncio") |
| 194 |
class TestCFreeThreading(TestFreeThreading, TestCase): |
| 195 |
|
| 196 |
def setUp(self): |
| 197 |
self._old_current_task = asyncio.current_task |
| 198 |
asyncio.current_task = asyncio.tasks.current_task = asyncio.tasks._c_current_task |
| 199 |
self._old_all_tasks = asyncio.all_tasks |
| 200 |
asyncio.all_tasks = asyncio.tasks.all_tasks = asyncio.tasks._c_all_tasks |
| 201 |
self._old_Task = asyncio.Task |
| 202 |
asyncio.Task = asyncio.tasks.Task = asyncio.tasks._CTask |
| 203 |
self._old_Future = asyncio.Future |
| 204 |
asyncio.Future = asyncio.futures.Future = asyncio.futures._CFuture |
| 205 |
return super().setUp() |
| 206 |
|
| 207 |
def tearDown(self): |
| 208 |
asyncio.current_task = asyncio.tasks.current_task = self._old_current_task |
| 209 |
asyncio.all_tasks = asyncio.tasks.all_tasks = self._old_all_tasks |
| 210 |
asyncio.Task = asyncio.tasks.Task = self._old_Task |
| 211 |
asyncio.Future = asyncio.futures.Future = self._old_Future |
| 212 |
return super().tearDown() |
| 213 |
|
| 214 |
|
| 215 |
def factory(self, loop, coro, **kwargs): |
| 216 |
return asyncio.tasks._CTask(coro, loop=loop, **kwargs) |
| 217 |
|
| 218 |
|
| 219 |
class TestEagerPyFreeThreading(TestPyFreeThreading): |
| 220 |
def factory(self, loop, coro, eager_start=True, **kwargs): |
| 221 |
return asyncio.tasks._PyTask(coro, loop=loop, **kwargs, eager_start=eager_start) |
| 222 |
|
| 223 |
|
| 224 |
@unittest.skipUnless(hasattr(asyncio.tasks, "_c_all_tasks"), "requires _asyncio") |
| 225 |
class TestEagerCFreeThreading(TestCFreeThreading, TestCase): |
| 226 |
def factory(self, loop, coro, eager_start=True, **kwargs): |
| 227 |
return asyncio.tasks._CTask(coro, loop=loop, **kwargs, eager_start=eager_start) |