Last active
July 2, 2021 04:48
-
-
Save JosephTLyons/fd09321257b755d4184b5599440df921 to your computer and use it in GitHub Desktop.
Python Threading Example
This file contains hidden or 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 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