Last active
June 18, 2017 11:20
-
-
Save vaclavcadek/e220d8035240a0100106fdb6ee0d7bad to your computer and use it in GitHub Desktop.
Asyncio vs. sync approach
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
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)) |
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
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