Use of multithreading in python
Recently I want to learn how to use pythonMultithreadingTo improve the efficiency of python in crawler projects.
Now we can find on the webpage that the use of multithreading in python is mostly used.ThreadingModule, but python also has a module calledThreadModule. Compared with the two, threading is a more advanced application module, but there must be reasons for the existence of thread. This article mainly introducesThreadApplications in python.
Resources come from a Summary of the python Development Documentation: https://docs.python.org/2/library/thread.html? Highlight = thread # module-thread
The thread module provides a lower-layer multi-threaded processing module (also known as a lightweight Processing Program), and supports synchronization and simple locks (mutexes or binary semaphores.
After importing thread in IDLE, you can use help (thread) to view related class functions and interface functions of thread. In the help document, we mainly see some lock related to thread, description of functions related to error and thread.
Main functions:The thread object has the following functions:
Thread. interrupt_main (): Raises a KeyboardInterrupt (usually Ctrl + C or delete) exception in the main thread. The subthread can interrupt the main thread through this function.
Thread. exit ():
The SystemExit exception is triggered. If it is not captured (no processing is performed), the termination of the thread will be affected.
Thread. allocate_lock ():
Return a new lock object. The lock method will be described below. This lock is not locked at first.
Thread. get_ident ():
Returns the ID of the current thread. The returned value is a non-zero integer. Its value has no direct meaning.
Thread. stack_size ([size]):
The stack size is returned when a new thread is created.
The Lock object has the following function description:
Lock. acquire ([waitflag]):
When this function does not have any optional parameters in parentheses, this method can capture the lock unconditionally. If this lock is occupied, this method will wait for it to be captured after it is released. If an optional parameter of the int type exists, the function behavior is related to the int value:
If the value is 0, the lock is directly captured when the lock can be captured unconditionally. When the waitflag is not 0, the lock will be unconditionally captured as before. If the lock is captured successfully, the return value is True. Otherwise, the return value is False.
Lock. release ():
Release the current lock Object. Here, the lock must be previously captured, but it should not be captured by the same lock.
Lock. locked ():
Returns the current status. If the current lock is captured, the return value is True; otherwise, the return value is False.
In addition, for these methods, the lock object can also be used through the with declaration:
Import thread # import thread module a_lock = thread. allocate_lock () with a_lock: print a_lock is locked while this executes
Assign a lock:Allocate_lock ()->Lock object(Allocate () is an obsolute synonym)
Operation lock method:
2) thread-related functions:
Create a new thread: start_new_thread (function, args [, kwargs]) (start_new () is an obsolete synonym)
Exit thread: exit () and exit_thread () (PyThread_exit_thread () is an obsolete synonym)
ILockRelated functionsExample(Skip error ):
import threaddef print_status(a_lock): if a_lock.locked(): print locked else: print not lockeda_lock = thread.allocate_lock()print_status(a_lock)a_lock.acquire()print_status(a_lock)a_lock.release()print_status(a_lock)
IIThreadRelated functionsExample:
import threaddef run(n): # a back door, can not be run 4 times if n == 4: thread.exit() for i in range(n): print ithread.start_new_thread(run,(5,))
3. Solve a synchronization problem
Try to solve the following synchronization problem: Use two threads to output "Hello" and "World" five times each, starting with "Hello" and ending with "World.
①HelloWorldProblem Synchronization Model Creation:
semaphore h = 1, w = 0 # because the semaphore w is 0,so we should acquire the lock w to let it be zero(locked) when we use python to .thread1(){ while(true) { p(h) do something; v(w) }}thread2(){ while(true) { p(w) do something; v(h) }}
② Use Python to implement the above Synchronization Model. The two solutions are as follows.
Solution A uses the main thread and another thread to print data alternately.
Solution B uses the other two threads except the main thread to print "Hello" and "World" alternately ".
import threaddef world(): for i in range(5): w_lock.acquire() # i want to print world print world h_lock.release() # you can print hello now w_lock.release()# main threadprint use two threads to print hello&worldh_lock = thread.allocate_lock()w_lock = thread.allocate_lock()w_lock.acquire(); # print world can not be started firstthread.start_new_thread(world,())for i in range(5): h_lock.acquire() print hello w_lock.release()# raw_input(finished)
import threaddef hello(): for i in range(5): h_ok.acquire() print hello w_ok.release()def world(): for i in range(5): w_ok.acquire() print world h_ok.release()# main threadprint use two threads to print hello&worldh_ok = thread.allocate_lock()w_ok = thread.allocate_lock()w_ok.acquire()thread.start_new_thread(hello,())thread.start_new_thread(world,())raw_input(finished) # !!it is necessary,in case main thread exit too early