Message400175
> weakref.WeakSet is not thread-safe, which means concurrent create_task's in different threads (even on separate event loops) is not safe.
actually it looks like WeakSet is *supposed* to be thread-safe
```
import patchy
patchy.patch(
"weakref:WeakSet._commit_removals",
"""\
@@ -1,5 +1,10 @@
def _commit_removals(self):
- l = self._pending_removals
+ pop = self._pending_removals.pop
discard = self.data.discard
- while l:
- discard(l.pop())
+ while True:
+ try:
+ item = pop()
+ except IndexError:
+ return
+ else:
+ discard(item)
"""
)
import itertools
import asyncio
import concurrent.futures
import sys
import threading
threads = 200
def test_all_tasks_threading() -> None:
async def foo() -> None:
await asyncio.sleep(0)
async def create_tasks() -> None:
for i in range(1000):
asyncio.create_task(foo())
await asyncio.sleep(0)
results = []
with concurrent.futures.ThreadPoolExecutor(threads) as tpe:
for f in concurrent.futures.as_completed(
tpe.submit(asyncio.run, create_tasks()) for i in range(threads)
):
results.append(f.result())
assert results == [None] * threads
def main():
for i in itertools.count():
test_all_tasks_threading()
print(f"worked {i}")
return 0
if __name__ == "__main__":
sys.exit(main())
``` |
|
Date |
User |
Action |
Args |
2021-08-23 20:12:53 | graingert | set | recipients:
+ graingert, asvetlov, yselivanov, bjs |
2021-08-23 20:12:53 | graingert | set | messageid: <1629749573.78.0.787559649536.issue44962@roundup.psfhosted.org> |
2021-08-23 20:12:53 | graingert | link | issue44962 messages |
2021-08-23 20:12:53 | graingert | create | |
|