Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
Add Comment
Please, Sign In to add comment