Skip to content

Instantly share code, notes, and snippets.

@adminy
Last active August 17, 2020 14:17
Show Gist options
  • Save adminy/8e8415392fdc2486c6553b4330cb486a to your computer and use it in GitHub Desktop.
Save adminy/8e8415392fdc2486c6553b4330cb486a to your computer and use it in GitHub Desktop.
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor(max_workers = 8) as executor:
tasks = range(20)
future_to_task = {executor.submit(lambda task: task ** 2, task): task for task in tasks}
for future in concurrent.futures.as_completed(future_to_task):
task = future_to_task[future]
output = future.result()
print(task, output)
@adminy
Copy link
Author

adminy commented Aug 17, 2020

import multiprocessing
from tqdm import tqdm
import threading
import time
THREADS = multiprocessing.cpu_count()
def task(output, n, i):
    for x in tqdm(range(i, n, THREADS)):
       #do task here
       output[x] = x * x

output = {}
threads = []
for i in range(THREADS):
    thread = threading.Thread(target=task, args=(output, n, i))
    thread.start()
    threads.append(thread)
    time.sleep(1)

for thread in threads: thread.join()
print('finished')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment