daily pastebin goal
69%
SHARE
TWEET

Untitled

a guest Jan 20th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from concurrent.futures import ProcessPoolExecutor
  2. import time
  3. import random
  4.  
  5. def long_task(sleeptime):
  6.     print("Sleeping {}".format(sleeptime))
  7.     time.sleep(sleeptime)
  8.     if sleeptime < 1:
  9.         return []
  10.     if random.random() > 0.7:
  11.         return [ sleeptime*0.5, sleeptime*2 ]
  12.     return [ sleeptime*0.5 ]
  13.  
  14.  
  15. class DazMainThing:
  16.  
  17.     def __init__(self):
  18.         self.executor = ProcessPoolExecutor()
  19.         self.running_futures = []
  20.  
  21.     def future_done(self, fut):
  22.         self.running_futures.remove(fut)
  23.         res = fut.result()
  24.         print("future result {}".format(res))
  25.         for st in res:
  26.             self.add_long_task(st)
  27.  
  28.     def add_long_task(self, sleeptime):
  29.         print("Adding new task")
  30.         fut = self.executor.submit(long_task, sleeptime)
  31.         fut.add_done_callback(self.future_done)
  32.         self.running_futures.append(fut)
  33.  
  34.     def executor_shutdown(self):
  35.         print("Waiting for tasks to complete")
  36.         # This could also be done by just passing wait=True to the shutdown method but if you want to do something special while waiting
  37.         if self.running_futures:
  38.             while True:
  39.                 all_done = True
  40.                 for sf in self.running_futures:
  41.                     if not sf.done():
  42.                         all_done = False
  43.                         break
  44.                 if all_done:
  45.                     break
  46.         self.executor.shutdown()
  47.         self.executor = None
  48.         self.running_futures = []
  49.  
  50.  
  51. if __name__ == '__main__':
  52.     ins = DazMainThing()
  53.     ins.add_long_task(5)
  54.     # Wait for tasks to complete (in reality this would be mainloop of the real application)
  55.     while True:
  56.         time.sleep(1)
  57.         if not ins.running_futures:
  58.             break
  59.     # This will again make sure there are no leftover tasks
  60.     ins.executor_shutdown()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top