Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python concurrent futures map test playground
#!/usr/bin/env python3
# concurrent_map_test.py
import random
import pendulum
import time
from concurrent import futures
from functools import reduce
def fakeFunc(base):
sleep_time = random.randint(1, 7)
time.sleep(sleep_time)
curr_time = pendulum.now(tz="UTC")
print(f" ..done for {base}")
return base
def main():
with futures.ThreadPoolExecutor() as executor:
listy = [1,2,3,4,5,6,7]
print("-- Using .submit --")
print(f"start time {pendulum.now(tz='UTC').strftime('%H:%M:%S')}")
promises = []
for i in listy:
promise = executor.submit(fakeFunc, i)
promises.append(promise)
print(f"ready for next {pendulum.now(tz='UTC').strftime('%H:%M:%S')}")
# as_completed will block
for promise in futures.as_completed(promises):
print(f"{promise.result()}")
print(f"end time {pendulum.now(tz='UTC').strftime('%H:%M:%S')}\n\n")
print("-- Using .map --")
test2 = 0
print(f"start time {pendulum.now(tz='UTC').strftime('%H:%M:%S')}")
# map releases results in order
for res in executor.map(fakeFunc, listy):
test2 += 1
print(f"#{test2} - {res}")
print(f"end time {pendulum.now(tz='UTC').strftime('%H:%M:%S')}\n\n")
print("-- Summing results --")
print(f"start time {pendulum.now(tz='UTC').strftime('%H:%M:%S')}")
promises = []
for i in listy:
promise = executor.submit(fakeFunc, i)
promises.append(promise)
results = map(lambda p: p.result(), promises)
print(f"results: {results}")
total = reduce(lambda acc, i: acc+i, results)
print(f"total: {total}")
print(f"end time {pendulum.now(tz='UTC').strftime('%H:%M:%S')}\n\n")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment