Skip to content

Instantly share code, notes, and snippets.

@amireldor
Created March 1, 2016 21:27
Show Gist options
  • Save amireldor/1d91968ce57fb0efb6e1 to your computer and use it in GitHub Desktop.
Save amireldor/1d91968ce57fb0efb6e1 to your computer and use it in GitHub Desktop.
How to make sure you run the same number of function asynchronically, adding a new one when an old one finishes.
import concurrent.futures
from concurrent.futures import FIRST_COMPLETED, ALL_COMPLETED
import time
from random import random
def do_magic(title):
time.sleep(random() * 3)
print("Bye", title)
class MagicDoer:
def __init__(self):
self.futures = []
self.executor = concurrent.futures.ThreadPoolExecutor(max_workers=4)
self.number = 0
def add_magic(self):
self.number += 1
f = self.executor.submit(do_magic, self.number)
self.futures.append(f)
def run(self):
# Startup
for x in range(4):
self.add_magic()
# Loop, create new ones until death
while self.number < 10:
waited = concurrent.futures.wait(self.futures, timeout=30, return_when=FIRST_COMPLETED)
for d in waited.done:
self.futures.remove(d) # remove old
self.add_magic()
# Wait on the remaining Futures
concurrent.futures.wait(self.futures, timeout=30, return_when=ALL_COMPLETED)
print('OK')
def main():
MagicDoer().run()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment