Skip to content

Instantly share code, notes, and snippets.

@dboyliao
Created February 20, 2017 07:32
Show Gist options
  • Save dboyliao/2751c1835bb01fa6ae1be00186516563 to your computer and use it in GitHub Desktop.
Save dboyliao/2751c1835bb01fa6ae1be00186516563 to your computer and use it in GitHub Desktop.
python3 futures module example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Example of `futures` module in python3
"""
from __future__ import print_function
import sys
from time import time
# check version
if sys.version_info.major < 3:
print("Please use python3")
sys.exit(1)
from concurrent.futures import ProcessPoolExecutor, as_completed
def job(num1, num2):
"""
worker
"""
print("working on {}".format((num1, num2)))
return num1 + num2
TO_DO = [[num1 for num1 in range(100)], [num2 for num2 in range(100)]]
START_TIME = time()
# synchronize version
with ProcessPoolExecutor(max_workers=None) as executor:
print("setup executor time: {}".format(time() - START_TIME))
for result in executor.map(job, TO_DO[0], TO_DO[1]):
print("map result: {}".format(result))
# asynchronize version
futures = {}
with ProcessPoolExecutor(max_workers=None) as executor:
for num1, num2 in zip(*TO_DO):
future = executor.submit(job, num1, num2)
futures[future] = (num1, num2)
for future in as_completed(futures):
try:
data = future.result()
except Exception as exp:
print("exception: {}".format(exp))
else:
print("{}: {}".format(futures[future], data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment