Created
April 14, 2016 11:39
-
-
Save h0rn3t/e8487f14ad4f09e039ae77a3441d1329 to your computer and use it in GitHub Desktop.
Sample code demonstrating use of await with concurrent.futures
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
from concurrent.futures import ThreadPoolExecutor, Future | |
import asyncio | |
import time | |
async def sum_numbers(x, y): | |
await asyncio.sleep(1) | |
return x + y | |
def sum_numbers_blocking(x, y): | |
time.sleep(1) | |
return x + y | |
async def print_sums(): | |
result = await sum_numbers(1, 2) | |
print('sum 1: %d' % result) | |
result = await executor.submit(sum_numbers_blocking, 3, 4) | |
print('sum 2: %s' % result) | |
executor = ThreadPoolExecutor() | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(print_sums()) | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment