1. The role of multi-threading
In short, multi-threading is the parallel processing of independent sub-tasks, thereby greatly improving the efficiency of the entire task.
2. Multi-threading related modules and methods in Python
Python provides several modules for multi-threaded programming, including thread, threading, Queue, etc.
The thread module provides basic thread and lock support. In addition to generating threads, it also provides basic synchronization data structure lock objects, including:
start_new_thread(function, args kwargs=None) Generates a new thread to run the given function
allocate_lock() allocates a lock object of type LockType
exit() lets the thread exit
acquire(wait=None) attempts to acquire the lock object
locked() returns TRUE if the lock object is acquired, otherwise returns FALSE
release() Release the lock
threading provides higher-level, more powerful thread management functions
Thread class represents the execution object of a thread
Lock lock primitive object
RLock is a reentrant lock object, allowing a single thread to obtain the acquired lock again
The queue module allows users to create a queue data structure that can be used to share data between multiple threads
Can be used for inter-process communication to share data between threads
Module function queue(size) creates a Queue object with size size
The queue object function qsize() returns the queue size
empty() returns True if the queue is empty, otherwise returns False
put(item, block=0) Put ITEM into the queue. If block is not 0, the function will block until it is in the queue
get(block=0) takes an object from the queue. If block is given, the function will block until there is an object in the queue
3. Example
Currently, Python's lib provides two startup methods for multi-thread programming. One is the more basic start_new_thread method in the thread module, which runs a function in the thread. The other is to use the thread object Thread class of the integrated threading module. .
What is currently used is to call the start_new_thread() function in the thread module in the old version to generate a new thread
In comparison, the implementation mechanism of thread.start_new_thread(function,(args[,kwargs])) is actually more similar to C, where the function parameter is the thread function to be called; (args[,kwargs]) is the thread function to be called. Create a tuple type of parameters for the thread function, where kwargs is an optional parameter. The newly created thread generally exits automatically when the execution of the thread function ends, or calls thread.exit() in the thread function to throw a SystemExit exception to achieve the purpose of thread exit.
print "=======================thread.start_new_thread启动线程=============" import thread #Python的线程sleep方法并不是在thread模块中,反而是在time模块下 import time def inthread(no,interval): count=0 while count<10: print "Thread-%d,休眠间隔:%d,current Time:%s"%(no,interval,time.ctime()) #使当前线程休眠指定时间,interval为浮点型的秒数,不同于Java中的整形毫秒数 time.sleep(interval) #Python不像大多数高级语言一样支持++操作符,只能用+=实现 count+=1 else: print "Thread-%d is over"%no #可以等待线程被PVM回收,或主动调用exit或exit_thread方法结束线程 thread.exit_thread() #使用start_new_thread函数可以简单的启动一个线程,第一个参数指定线程中执行的函数,第二个参数为元组型的传递给指定函数的参数值 thread.start_new_thread(inthread,(1,2)) #线程执行时必须添加这一行,并且sleep的时间必须足够使线程结束,如本例 #如果休眠时间改为20,将可能会抛出异常 time.sleep(30) '''
When starting a thread using this method, exceptions may occur
Unhandled exception in thread started by Error in sys.excepthook: Original exception was:
Solution: After starting a thread, make sure that the main thread waits for all sub-threads to return results before exiting. If the main thread ends earlier than the sub-threads, regardless of whether the sub-threads are background threads, they will be interrupted and this exception will be thrown.
If there is no response to the blocking wait, in order to prevent the main thread from exiting early, time.sleep must be called to make the main thread sleep for a long enough time. In addition, a locking mechanism can also be used to avoid similar situations. When starting the thread, give each thread A lock is added until the thread is running, and then the lock is released. At the same time, a while loop is used in Python's main thread to continuously determine that each thread lock has been released.
import thread; from time import sleep,ctime; from random import choice #The first param means the thread number #The second param means how long it sleep #The third param means the Lock def loop(nloop,sec,lock): print "Thread ",nloop," start and will sleep ",sec; sleep(sec); print "Thread ",nloop," end ",sec; lock.release(); def main(): seconds=[4,2]; locks=[]; for i in range(len(seconds)) : lock=thread.allocate_lock(); lock.acquire(); locks.append(lock); print "main Thread begins:",ctime(); for i,lock in enumerate(locks): thread.start_new_thread(loop,(i,choice(seconds),lock)); for lock in locks : while lock.locked() : pass; print "main Thread ends:",ctime(); if __name__=="__main__" : main();
Many introductions say that the Threading module is recommended in new python versions, but it has not been applied yet. . .