Home > Article > Backend Development > How to use the threading module for multi-thread management in Python 3.x
How to use the threading module for multi-thread management in Python 3.x
Introduction:
In the computer field, multi-threading is an important programming model that can improve the concurrency and execution efficiency of the program. . The Python language provides the threading module to facilitate developers to manage multi-threads. This article will introduce how to use the threading module for multi-thread programming and demonstrate the use of multi-threading through examples.
import threading count = 0 # 共享资源 lock = threading.Lock() # 互斥锁 def increase(): global count for _ in range(100000): lock.acquire() # 加锁 count += 1 lock.release() # 解锁 def decrease(): global count for _ in range(100000): lock.acquire() # 加锁 count -= 1 lock.release() # 解锁 if __name__ == '__main__': # 创建两个线程 t1 = threading.Thread(target=increase) t2 = threading.Thread(target=decrease) # 启动线程 t1.start() t2.start() # 等待线程结束 t1.join() t2.join() # 输出结果 print("count:", count)
In the above example, we created two threads t1 and t2, calling the increase() and decrease() functions respectively to operate the shared resource count. Since Lock is used, no conflict occurs. Finally, the value of the result count is output.
import threading count = 0 # 共享资源 lock = threading.Lock() # 互斥锁 condition = threading.Condition() # 条件变量 def produce(): global count while True: with condition: if count >= 10: condition.wait() # 释放锁并等待条件变量 count += 1 print("Produced 1 item") condition.notify() # 通知等待的线程 def consume(): global count while True: with condition: if count <= 0: condition.wait() # 释放锁并等待条件变量 count -= 1 print("Consumed 1 item") condition.notify() # 通知等待的线程 if __name__ == '__main__': # 创建两个线程 t1 = threading.Thread(target=produce) t2 = threading.Thread(target=consume) # 启动线程 t1.start() t2.start() # 等待线程结束 t1.join() t2.join()
In the above example, we created two threads t1 and t2, calling the produce() and consume() functions respectively to simulate the producer and consumer scenarios. By using the Condition class, synchronization and communication between threads are achieved. When the counter count does not meet the condition, the thread waits and continues to execute other threads until the condition is met, notifying the waiting thread.
Summary:
This article introduces how to use the threading module for multi-thread management in Python 3.x. The basic operations of multi-threading and the use of thread synchronization are demonstrated through sample code. Reasonable use of multi-threading can improve program execution efficiency and concurrency, but at the same time, attention must be paid to thread safety and data sharing issues. In actual applications, just choose the appropriate multi-threading solution according to specific needs.
The above is the detailed content of How to use the threading module for multi-thread management in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!