Last active
August 5, 2020 11:15
-
-
Save easysugar/ef454d4e22b35198761c2dd7c8ff486b to your computer and use it in GitHub Desktop.
Using async functions in mutliprocessing
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 | |
from concurrent.futures import ProcessPoolExecutor, as_completed | |
async def func(): | |
pass | |
def process_main(*args, **kwargs): | |
loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(loop) | |
loop.run_until_complete(func(*args, **kwargs)) | |
if __name__ == '__main__': | |
with ProcessPoolExecutor(num_processes) as executor: | |
futures = [executor.submit(process_main) for i in range(num_processes)] | |
for f in as_completed(futures): | |
f.result() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment