Created
January 20, 2019 10:10
-
-
Save rambo/8f46faf25df25a79f2cb7cf7ae32f432 to your computer and use it in GitHub Desktop.
concurrent.futures example where futures can generate more work to do
This file contains 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.futures import ProcessPoolExecutor | |
import time | |
import random | |
def long_task(sleeptime): | |
print("Sleeping {}".format(sleeptime)) | |
time.sleep(sleeptime) | |
if sleeptime < 1: | |
return [] | |
if random.random() > 0.7: | |
return [ sleeptime*0.5, sleeptime*2 ] | |
return [ sleeptime*0.5 ] | |
class DazMainThing: | |
def __init__(self): | |
self.executor = ProcessPoolExecutor() | |
self.running_futures = [] | |
def future_done(self, fut): | |
self.running_futures.remove(fut) | |
res = fut.result() | |
print("future result {}".format(res)) | |
for st in res: | |
self.add_long_task(st) | |
def add_long_task(self, sleeptime): | |
print("Adding new task") | |
fut = self.executor.submit(long_task, sleeptime) | |
fut.add_done_callback(self.future_done) | |
self.running_futures.append(fut) | |
def executor_shutdown(self): | |
print("Waiting for tasks to complete") | |
# This could also be done by just passing wait=True to the shutdown method but if you want to do something special while waiting | |
if self.running_futures: | |
while True: | |
all_done = True | |
for sf in self.running_futures: | |
if not sf.done(): | |
all_done = False | |
break | |
if all_done: | |
break | |
self.executor.shutdown() | |
self.executor = None | |
self.running_futures = [] | |
if __name__ == '__main__': | |
ins = DazMainThing() | |
ins.add_long_task(5) | |
# Wait for tasks to complete (in reality this would be mainloop of the real application) | |
while True: | |
time.sleep(1) | |
if not ins.running_futures: | |
break | |
# This will again make sure there are no leftover tasks | |
ins.executor_shutdown() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Forgot example output