gist.github.com/lars-tiede/01e5f5a551f29a5f300e
asyncio + multithreading: one asyncio event loop per thread
asyncio + multithreading: one asyncio event loop per thread - asyncio_loops.py
gist.github.com
ex1
import asyncio
import threading
import random
def thr(i):
# we need to create a new loop for the thread, and set it as the 'default'
# loop that will be returned by calls to asyncio.get_event_loop() from this
# thread.
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(do_stuff(i))
loop.close()
async def do_stuff(i):
await asyncio.sleep(random.uniform(0.1, 0.5)) # NOTE if we hadn't called
# asyncio.set_event_loop() earlier, we would have to pass an event
# loop to this function explicitly.
print(i)
def main():
num_threads = 10
threads = [ threading.Thread(target = thr, args=(i,)) for i in range(num_threads) ]
[ t.start() for t in threads ]
[ t.join() for t in threads ]
print("bye")
if __name__ == "__main__":
main()
ex2
import asyncio
import random
from concurrent.futures import ThreadPoolExecutor, as_completed
def thr(i):
# we need to create a new loop for the thread, and set it as the 'default'
# loop that will be returned by calls to asyncio.get_event_loop() from this
# thread.
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
ret = loop.run_until_complete(do_stuff(i))
loop.close()
return ret
async def do_stuff(i):
ran = random.uniform(0.1, 0.5)
await asyncio.sleep(ran) # NOTE if we hadn't called
# asyncio.set_event_loop() earlier, we would have to pass an event
# loop to this function explicitly.
print(i, ran)
return ran
def main():
num_threads = 10
with ThreadPoolExecutor(num_threads) as executor:
futures = {}
for i in range(num_threads):
future = executor.submit(thr, i)
futures[future] = i
for future in as_completed(futures):
ret = future.result()
print(ret, futures[future])
if __name__ == "__main__":
main()
'시행착오 > [python]' 카테고리의 다른 글
[python] filter 함수 사용시 주의할 점 - iterator (0) | 2021.04.21 |
---|---|
[python] paramiko ssh 연결되어있는 상태인지 체크법 (0) | 2021.04.15 |
[python] __enter__, __exit__ context manager - 파이썬 with custom하기 (0) | 2021.04.02 |
python에서 json data다룰때 key가 존재하는지 확인하는 방법 (0) | 2021.03.31 |
파이썬 requests 비동기로 할 때 arguments 여러개 보내는 법 (0) | 2021.03.30 |