Created
May 31, 2020 12:10
-
-
Save Toshakins/9280a87b23a608be7526f51c8349a252 to your computer and use it in GitHub Desktop.
Event loop inside threads, Python
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 concurrent | |
import threading | |
from concurrent.futures import as_completed, ThreadPoolExecutor | |
from itertools import zip_longest | |
from random import random | |
async def subwork(i): | |
slept = random() % 1 | |
await asyncio.sleep(slept) | |
print('hello %s, slept %s from %s' % (i, slept, threading.current_thread())) | |
async def loopwork(workload): | |
multiload = [subwork(i) for i in workload] | |
await asyncio.gather(*multiload) | |
def threadwork(workload): | |
loop = asyncio.new_event_loop() | |
loop.run_until_complete(loopwork(workload)) | |
def chunks(iterable, n): | |
args = [iter(iterable)] * n | |
return zip_longest(*args, fillvalue=None) | |
def app(): | |
total = 100 | |
workload = list(range(total)) | |
threads = 4 | |
thread_load = chunks(workload, total // threads) | |
with ThreadPoolExecutor(max_workers=threads) as pool: | |
futures = [pool.submit(threadwork, x) for x in thread_load] | |
for f in concurrent.futures.as_completed(futures): | |
f.result() | |
print('ololo') | |
if __name__ == '__main__': | |
app() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment