Created
March 4, 2024 09:31
-
-
Save Brymes/7ba8eb4be7168ea6c13a40e8e5af987a to your computer and use it in GitHub Desktop.
Python Threading Snippet
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 threading | |
import time | |
# Function to be executed concurrently | |
def print_after_delay(text, delay): | |
time.sleep(delay) | |
print(text) | |
# Create multiple threads | |
thread1 = threading.Thread(target=print_after_delay, args=("Hello", 1)) | |
thread2 = threading.Thread(target=print_after_delay, args=("World", 2)) | |
# Start the threads | |
thread1.start() | |
thread2.start() | |
# Wait for threads to complete | |
thread1.join() | |
thread2.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment