Last active
August 17, 2020 14:17
-
-
Save adminy/8e8415392fdc2486c6553b4330cb486a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Author
adminy
commented
Aug 15, 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