Created
October 21, 2016 06:53
-
-
Save xmkevinchen/dd4b2b6a08a65f487a77035029af9c85 to your computer and use it in GitHub Desktop.
Python: Asyncio How to use blocking function
This file contains hidden or 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
# asyncio_executor_process.py | |
import asyncio | |
import concurrent.futures | |
import logging | |
import sys | |
import time | |
def blocks(n): | |
log = logging.getLogger('blocks({})'.format(n)) | |
log.info('running') | |
time.sleep(0.1) | |
log.info('done') | |
return n ** 2 | |
async def run_blocking_tasks(executor): | |
log = logging.getLogger('run_blocking_tasks') | |
log.info('starting') | |
log.info('creating executor tasks') | |
loop = asyncio.get_event_loop() | |
blocking_tasks = [ | |
loop.run_in_executor(executor, blocks, i) | |
for i in range(6) | |
] | |
log.info('waiting for executor tasks') | |
completed, pending = await asyncio.wait(blocking_tasks) | |
results = [t.result() for t in completed] | |
log.info('results: {!r}'.format(results)) | |
log.info('exiting') | |
# Configure logging to show the name of the thread where the log message originates | |
logging.basicConfig( | |
level=logging.INFO, | |
format='PID %(process)5s %(name)18s: %(message)s', | |
stream=sys.stderr, | |
) | |
# Create a limited thread pool. | |
executor = concurrent.futures.ProcessPoolExecutor(max_workers=3) | |
event_loop = asyncio.get_event_loop() | |
try: | |
event_loop.run_until_complete( | |
run_blocking_tasks(executor) | |
) | |
finally: | |
event_loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment