Skip to content

Instantly share code, notes, and snippets.

@tokibito
Created July 9, 2016 14:45
Show Gist options
  • Save tokibito/5b36eae0f2fdedb0ebefcc3a1181858b to your computer and use it in GitHub Desktop.
Save tokibito/5b36eae0f2fdedb0ebefcc3a1181858b to your computer and use it in GitHub Desktop.
Example for concurrent.futures
import os
import threading
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
from time import sleep
def writer(value):
sleep(1)
print("PID={}, ThreadID={}: {}".format(os.getpid(), threading.get_ident(), value))
def main():
# executor_class = ThreadPoolExecutor
executor_class = ProcessPoolExecutor
executor = executor_class(max_workers=3)
tasks = []
print("--start--")
for i in range(10):
tasks.append(executor.submit(writer, i))
while not all([t.done() for t in tasks]):
sleep(0.1)
print("--done--")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment