Skip to content

Instantly share code, notes, and snippets.

@t27
Last active April 21, 2020 12:45
Show Gist options
  • Save t27/c1d9ceb68bf5f18f24ef7e82d4b071a5 to your computer and use it in GitHub Desktop.
Save t27/c1d9ceb68bf5f18f24ef7e82d4b071a5 to your computer and use it in GitHub Desktop.
Script to show sample code(boilerplate) for concurrent execution using concurrent.futures.ThreadPoolExecutor in python 3 (mainly IO operations like network calls, ie. HTTP etc)
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
Thread_pool = ThreadPoolExecutor(6) # can change 6 to any number
def sample_sum_func(arg1,arg2):
print("start",arg1,arg2)
time.sleep(2)
print("end",arg1,arg2)
return arg1+arg2
# wait on the result from a single async thread
thread1 = Thread_pool.submit(sample_sum_func,arg1,arg2)
sum = thread1.result(timeout=5*60) # this will wait till thread1 completes or till "timeout" milliseconds
# for multiple
futures = []
for i in range(5):
futures.append(pool.submit(sample_sum_func, i+1, i+1))
for x in as_completed(futures):
doStuff(x.result()) # x is the result of the thread # try replacing doStuff with print for now
# If doStuff is simply the print function, sample out put looks like this
# start 1 1
# start 2 2
# start 3 3
# start 4 4
# start 5 5
# end 2 2
# end 4 4
# end 1 1
# end 5 5
# end 3 3
# 4
# 8
# 2
# 10
# 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment