Threading Basics

Creating and managing threads

Interview Relevant: Multithreading

Threading

run code concurrently in the same process.

Code Examples

python
1import threading
2import time
3
4def worker(n):
5    print(f"Worker {n} started")
6    time.sleep(1)
7    print(f"Worker {n} finished")
8
9t = threading.Thread(target=worker, args=(1,))
10t.start()
11t.join()  # Wait for it to finish

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In