Multithreading is a process of executing multiple threads (small units of a program) concurrently within a single process. This can greatly improve the performance of your program, as multiple threads can run simultaneously and make efficient use of the available processing power.
In Python, multithreading is achieved using the threading
module. In this module, the Thread
class represents a thread of execution, and provides a simple way to create and run threads in your program.
A Simple Example
Here's a simple example that demonstrates the basic usage of the threading
module:
import time
import threading
def worker():
print("Worker thread started")
time.sleep(1)
print("Worker thread finished")
thread = threading.Thread(target=worker)
thread.start()
print("Main thread finished")
In this example, we define a function worker
that simply prints a message and then sleeps for 1 second. We then create a Thread
object, passing worker
as the target function to be executed by the thread. Finally, we start the thread using the start
method.
When you run this program, you'll see that the Worker thread started
and Worker thread
finished messages are printed interleaved with the Main thread finished
message, as both threads run concurrently.
Sharing Data between Threads
In some cases, you may want to share data between threads. One simple way to do this is to use a global variable, like this:
import time
import threading
counter = 0
def worker():
global counter
print("Worker thread started")
for i in range(100000):
counter += 1
print("Worker thread finished")
thread = threading.Thread(target=worker)
thread.start()
time.sleep(1)
print("Counter value:", counter)
print("Main thread finished")
In this example, we define a global variable counter
that is incremented by the worker
thread. When the Main
thread finishes, it prints the final value of counter
, showing that the two threads have shared access to the same data.
Locking
However, be careful when sharing data between threads, as this can easily lead to race conditions, where multiple threads try to access and modify the same data simultaneously, leading to unpredictable results. To avoid this, you can use locks to synchronize access to shared data, like this:
import time
import threading
counter = 0
lock = threading.Lock()
def worker():
global counter
print("Worker thread started")
for i in range(100000):
with lock:
counter += 1
print("Worker thread finished")
thread = threading.Thread(target=worker)
thread.start()
time.sleep(1)
print("Counter value:", counter)
print("Main thread finished")
In this example, we use a Lock
object to synchronize access to the shared counter
variable. The with
statement ensures that the lock is acquired before the variable is modified, and released after the modification is complete. This ensures that only one thread can access the variable.
To Learn More About Python Multithreading: Click Here
Connect with me: Linkedin
Top comments (1)
Great article, thanks for sharing. Looking forward to learn more about python. I'm planning make a Twitter bot using Python soon..