Skip to content

Instantly share code, notes, and snippets.

@vaclavcadek
Last active June 18, 2017 11:20
Show Gist options
  • Save vaclavcadek/e220d8035240a0100106fdb6ee0d7bad to your computer and use it in GitHub Desktop.
Save vaclavcadek/e220d8035240a0100106fdb6ee0d7bad to your computer and use it in GitHub Desktop.
Asyncio vs. sync approach
from concurrent import futures
import time
def long_running_task(result):
print('Task is running!')
time.sleep(1)
return result
start = time.perf_counter()
with futures.ThreadPoolExecutor(12) as executor:
results = executor.map(long_running_task, range(5))
print(list(results))
end = time.perf_counter()
print('Total time: {t:.2f} s'.format(t=end - start))
import time
def long_running_task():
print('Task is running!')
time.sleep(1)
start = time.perf_counter()
for i in range(5):
long_running_task()
end = time.perf_counter()
print('Total time: {t:.2f} s'.format(t=end - start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment