-
-
Save takidog/c53f73e24295d66c76b5e330940bcf73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import threading | |
import time | |
from typing import TYPE_CHECKING | |
if TYPE_CHECKING: | |
from asyncio import AbstractEventLoop | |
async def foo(condition): | |
loop = asyncio.get_event_loop() | |
print("Foo loop id:", id(loop)) | |
a = 1 | |
while True: | |
print(f"{time.strftime('%X')}: do {a}") | |
a += 1 | |
async with condition: | |
await condition.wait() | |
def non_async_func(loop: "AbstractEventLoop", condition): | |
print("non_async_func loop id:", id(loop)) | |
time.sleep(1) | |
loop.create_task(release(condition)) | |
async def release(condition): | |
loop = asyncio.get_event_loop() | |
print("Release loop id:", id(loop)) | |
async with condition: | |
condition.notify_all() | |
print("notify") | |
async def main(): | |
loop = asyncio.get_event_loop() | |
print("main loop id:", id(loop)) | |
condition = asyncio.Condition() | |
job1 = loop.run_in_executor(None, non_async_func, loop, condition) | |
job2 = loop.create_task(foo(condition)) | |
await asyncio.gather(job1, job2) | |
if __name__ == "__main__": | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Version 2
more wrong