Created
March 23, 2022 02:11
-
-
Save epomatti/005ffc83919f7061f7f1334d03c7077d to your computer and use it in GitHub Desktop.
threads in python
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 time | |
import threading | |
def do_work(work): | |
print(f'Doing {work}') | |
time.sleep(3) | |
print(f'Finished {work}') | |
def do_all_the_work(work): | |
index = 0 | |
for piece_of_work in work: | |
t = threading.Thread(target=do_work, args=(piece_of_work,)) | |
t.start() | |
work[index] = t | |
index += 1 | |
work = ['Work-1', 'Work-2', 'Work-3', 'Work-4', 'Work-5'] | |
do_all_the_work(work) | |
for thread in work: | |
thread.join() | |
print('Finished all threads') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment