Skip to content

Instantly share code, notes, and snippets.

@biggers
Created August 10, 2017 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biggers/20752797e095dd992ee2d86f2a835f09 to your computer and use it in GitHub Desktop.
Save biggers/20752797e095dd992ee2d86f2a835f09 to your computer and use it in GitHub Desktop.
async Python3 code using blocking-function
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
import random
TPOOL_SIZE = 8
def blocking(pid):
""" a non-deterministic "blocking" ? task
"""
sleep_for = random.randint(0, 3) * 0.001
time.sleep(sleep_for)
return pid, sleep_for
def main():
pool = ThreadPoolExecutor(TPOOL_SIZE)
futures = []
for pid in range(TPOOL_SIZE):
futures.append(pool.submit(blocking, pid))
for x in as_completed(futures):
print(x.result())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment