mirror of
https://github.com/python/cpython.git
synced 2025-10-26 19:24:34 +00:00
gh-128002: use _PyObject_SetMaybeWeakref when creating tasks in asyncio (#128885)
This commit is contained in:
parent
fc6bc1e4e3
commit
8e0b36006c
2 changed files with 39 additions and 0 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import asyncio
|
||||
import threading
|
||||
import unittest
|
||||
from threading import Thread
|
||||
from unittest import TestCase
|
||||
|
|
@ -58,6 +59,38 @@ def runner():
|
|||
with threading_helper.start_threads(threads):
|
||||
pass
|
||||
|
||||
def test_all_tasks_different_thread(self) -> None:
|
||||
loop = None
|
||||
started = threading.Event()
|
||||
|
||||
async def coro():
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
lock = threading.Lock()
|
||||
tasks = set()
|
||||
|
||||
async def main():
|
||||
nonlocal tasks, loop
|
||||
loop = asyncio.get_running_loop()
|
||||
started.set()
|
||||
for i in range(1000):
|
||||
with lock:
|
||||
asyncio.create_task(coro())
|
||||
tasks = self.all_tasks(loop)
|
||||
|
||||
runner = threading.Thread(target=lambda: asyncio.run(main()))
|
||||
|
||||
def check():
|
||||
started.wait()
|
||||
with lock:
|
||||
self.assertSetEqual(tasks & self.all_tasks(loop), tasks)
|
||||
|
||||
threads = [threading.Thread(target=check) for _ in range(10)]
|
||||
threads.append(runner)
|
||||
|
||||
with threading_helper.start_threads(threads):
|
||||
pass
|
||||
|
||||
def test_run_coroutine_threadsafe(self) -> None:
|
||||
results = []
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue