Skip to content

Instantly share code, notes, and snippets.

@h0rn3t
Created April 14, 2016 11:39
Show Gist options
  • Save h0rn3t/e8487f14ad4f09e039ae77a3441d1329 to your computer and use it in GitHub Desktop.
Save h0rn3t/e8487f14ad4f09e039ae77a3441d1329 to your computer and use it in GitHub Desktop.
Sample code demonstrating use of await with concurrent.futures
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