Skip to content

Instantly share code, notes, and snippets.

@epomatti
Created March 23, 2022 02:11
Show Gist options
  • Save epomatti/005ffc83919f7061f7f1334d03c7077d to your computer and use it in GitHub Desktop.
Save epomatti/005ffc83919f7061f7f1334d03c7077d to your computer and use it in GitHub Desktop.
threads in python
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