Look at python multithreading ------ threading module, pythonthreading Module

Source: Internet
Author: User

Look at python multithreading ------ threading module, pythonthreading Module

Now let's record the notes that can be thought of with multithreading:

Threading module:

1. Questions about parameter passing

If the called sub-thread function requires passing parameters, add "," next to the parameter. Otherwise, a parameter Exception error is thrown.

As follows:

1     for i in xrange(5):2         threads.append(threading.Thread(target=worker,args=(i,)))

 

2. join () Blocking

Once the join () method is called, the thread will be blocked and the thread will execute itself only after other threads have completed execution. When we create n sub-threads in main thread A, it should be noted that the parent thread or sub-thread should be blocked as needed, and t. the join () position. Compare the following example:

----> Neither the child thread nor the parent thread is blocked.

1 #-*-coding: UTF-8-*-2 import Queue, time, threading 3 start = time. clock () 4 5 def worker (m): 6 print 'worker', m 7 time. sleep (1) 8 return 9 10 if _ name _ = "_ main _": 11 threads = [] 12 for I in xrange (5 ): 13 threads. append (threading. thread (target = worker, args = (I,) 14 for t in threads: 15 t. start () 16 # t. join () # block sub-thread 17 18 # t. join () # block parent thread 19 20 end = time. clock () 21 print "finished: %. 3fs "% (end-start)

 

Input:

Worker 0
Worker 1
Worker 2
Worker 3
Worker 4
Finished: 0.001 s

Here, the parent thread has actually ended, because finished: 0.001 s has been printed, but the sub-thread has not been executed completely. After 1 second of sleep, the end mark of the program "Process finished with exit code 0" appears.

  

-----> In the same Code, when the sub-thread is blocked, the output is as follows:

Worker 0
Worker 1
Worker 2
Worker 3
Worker 4
Finished: 5.004 s

Here, because the five sub-threads have just been replaced by t. after start (), it immediately blocks itself, so they will be executed in order. Similarly, the program sleep for 5 seconds, you must note that, the program's efficiency is not improved, and it still takes 5 seconds. So there is a problem. The problem lies in t. where the join () code is placed, it should be for t in threads: t. join () so that these threads start at the same time and then join () at the same time ().

 

-------> In the same Code, when the parent thread is blocked, the output is as follows:

Worker 0
Worker 1
Worker 2
Worker 3
Worker 4
Finished: 1.003 s

Here, the parent thread is blocked, and the parent thread will wait until the child thread ends to continue running and print finished. The program efficiency is also improved. This is equivalent to the above mentioned. First, start all the sub-threads and then join them.

 

3. The setDaemon () method

The setDaemon () method is set in the sub-thread. After we create n sub-threads in parent thread A and set setDaemon (True) for our favorite sub-thread, after their parent threads are running, the program is directly ended no matter whether the Sub-threads are running or not. Note that the setDaemon () method must be set before the start () method. Otherwise, a RuntimeError exception is thrown.

The following example is also used:

1 #-*-coding: UTF-8-*-2 import Queue, time, threading 3 start = time. clock () 4 5 def worker (m): 6 print 'worker', m 7 time. sleep (1) 8 return 9 10 if _ name _ = "_ main _": 11 threads = [] 12 for I in xrange (5 ): 13 threads. append (threading. thread (target = worker, args = (I,) 14 for t in threads: 15 t. setDaemon (True) 16 t. start () 17 # t. join () # block sub-thread 18 19 # t. join () # block parent thread 20 21 end = time. clock () 22 print "finished: %. 3fs "% (end-start)

 

Here, the parent thread is not blocked and the output is as follows:

Worker 0
Worker 1
Worker 2
Worker 3
Worker 4
Finished: 0.001 s

Note: once the main thread ends, the sub-thread's memory will be recycled directly to end the entire process. If sleep 1 of the sub-process is not executed, it exits. This should be useful for some application scenarios of auxiliary sub-threads.

 

4. Two ways to create a sub-Thread

The first is the function (worker) mentioned above, which creates the sub-Thread to execute, and then passes the function to the threading. Thread object for execution;

The second method is to directly inherit from the threading. Thread class and create a new class. By Rewriting the run () method in the new class, implement the content to be executed by the sub-Thread, for example:

 1 # -*- coding:utf-8 -*- 2 __author__ = 'webber' 3 import threading,time 4  5 class Mythread(threading.Thread): 6  7     def __init__(self,m): 8         threading.Thread.__init__(self) 9         self.m = m10 11     def run(self):12         print 'worker', self.m13         time.sleep(1)14         return15 16 if __name__ == "__main__":17     start = time.clock()18 19     threads = []20     for i in xrange(5):21         threads.append(Mythread(i))22     for t in threads:23         t.start()24 25     t.join()26     end = time.clock()27     print "finished: %.3fs" % (end - start)

The output is the same as the above main thread blocking result.

Pay attention to the yellow part, the parameter passing method when calling.

 

5. Locking ----> Lock, RLock, and Condition methods

As mentioned earlier, python theoretically cannot implement true multithreading. Even if you have multiple CPUs, python multithreading can only use one, to prevent the embarrassment of modifying the data in the shared data space in multiple threads, the threading module inherits the Lock method of the thread module, which is the simplest Lock and easy to implement, you only need to add the lock and release lock before and after modifying the data in the Child thread.

There are three sentences:

A. Create a lock object in the main function: for example, Lock = threading. Lock () # Return a new lock object and create a Lock.

B. Before the sub-thread needs to modify the data, lock. acquire () # obtain the lock

C. After the sub-thread modifies the data, lock. acquire () # Release the lock.

The following is an example of code application:

1 #-*-coding: UTF-8-*-2 _ author _ = 'webber' 3 import threading, time, random 4 5 dish = 0 6 lock = threading. lock () 7 8 9 def producerFunction (): 10''' if the screener is larger than 0.5, add an apple '''11 global lock to the plate, dish12 while dish <10:13 if (random. random ()> 0.5): 14 lock. acquire () 15 dish + = 116 print ('producer added an apple, now there are % d Apple '% (dish,) 17 lock. release () 18 time. sleep (random. random () * 3) 19 20 21 def consumerFunction (): 22 ''' if the screener is smaller than 0.5, take an apple ''' 23 global lock from the plate, dish24 while dish> 0: 25 if (random. random () <0.5): 26 lock. acquire () 27 dish-= 128 print ('consumer takes an apple now, there are % d Apple now '% (dish,) 29 lock. release () 30 time. sleep (random. random () * 3) 31 32 33 def begin (): 34 ident1 = threading. thread (target = producerFunction () 35 ident2 = threading. thread (target = consumerFunction () 36 ident1.start () 37 ident2.start () 38 39 40 if _ name _ = '_ main _': 41 begin ()
View Code

 

Secondly, the threading module proposes a more advanced Lock RLock, which is used to solve the deadlock problem that may occur in the Lock, that is, when due to negligence, the same lock object in a sub-thread may be acquire () twice in a row. Because the first acquire has no release, the second acquire request will suspend the sub-thread, the lock object will never be release, resulting in a deadlock. The RLock object allows a thread to perform the acquire operation multiple times, and maintains the number of acquire threads through the counter variable internally, each acquire operation must have a release operation. After all the release operations are completed, other threads can apply for the RLock object. For the moment, I tried it as a Lock method and passed it .~~~

 

Finally, the threading module provides more advanced encapsulation, which is an advanced multi-thread synchronization method, including threading. event and threading. condition, where threading. event is a simple synchronization method. When a process is marked as event, other processes need to wait. The following methods are used:

Event. wait ([timeout]) The thread is blocked until the internal identifier of the Event object is set to True or times out (if the timeout parameter is provided)
Event. set () Set the ID number to True.
Event. clear () Set as identifier False

 

 

 

Threading. condition can be understood as a more advanced lock. It provides more advanced functions than RLock, allowing us to control complicated thread synchronization problems, it maintains a lock object internally (RLock by default), which can be passed as a parameter when a Condition object is created. Condition also provides the acquire and release methods, which feature the internal wait and notify mechanisms. For details, see the threading module. The following methods can be called only after the object gets the lock. Otherwise, runtimeError is thrown.

Condition. wait ([timeout]): The wait method releases the internal occupation and threads are suspended until the received notification is awakened or timed out (if the timeout parameter is provided ). When the thread is awakened and occupied again, the program will continue to run.
Condition. Policy () Wake up a suspended thread (if a suspended thread exists ). Note: The notify () method does not release the occupied objects.
Condition. policy_all () Wake up all suspended threads (if any ). Note: These methods do not release the occupied items.

 

 

 

 

Reference: http://orangeholic.iteye.com/blog/1720421

 

6. Other Methods

Since threading is inherited from the thread module, there are still some common attribute methods, such:

T. getName (): gets the name of the subthread. The default value is Tread-n (n is the thread serial number)

T. setName (): Set the subthread name.

T. ident: gets the thread identifier, which must be called after t. start (); otherwise, None is returned.

T. is_alive (): determines whether the Sub-thread is activated and returns True or False.

 

For details about Semaphore, event, and Condition instances, try again later. Refer to this blog:

Http://www.jb51.net/article/57672.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.