Skip to content

Instantly share code, notes, and snippets.

@JosephTLyons
Last active July 2, 2021 04:48
Show Gist options
  • Select an option

  • Save JosephTLyons/fd09321257b755d4184b5599440df921 to your computer and use it in GitHub Desktop.

Select an option

Save JosephTLyons/fd09321257b755d4184b5599440df921 to your computer and use it in GitHub Desktop.
Python Threading Example
import threading
import time
lock = threading.Lock()
def make_cookies(cake_number):
"""Instructions for the robot on how to create cookies"""
with lock:
print(f"Cake #: {cake_number}")
print("===============")
steps = [
"Buy ingredients",
"Mix ingredients",
"Spray cooking sheet",
"Form Cookies on cooking sheet",
"Bake",
"Remove from oven",
"Let cool",
"Enjoy",
]
for step_number, step in enumerate(steps, 1):
print(f"{step_number}: {step}")
print()
time.sleep(1)
def main():
i = 1
while i < 1001:
threads = []
for j in range(0, 50):
thread = threading.Thread(target=make_cookies, args=(i,))
thread.start()
threads.append(thread)
i += 1
for thread in threads:
thread.join()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment