Python multi-thread and Python lock, python multi-thread

Source: Internet
Author: User
Tags bbcode

Python multi-thread and Python lock, python multi-thread

Python Multithreading 
There are two methods to implement multithreading in Python. One is the start_new_thread () function based on the _ thread module (thread module in Python2.x with no underline, another Thread class based on the threading module.
In fact, multi-thread programming in Python cannot really use multi-core CPUs, but use open-source modules to distribute your computing pressure to multi-core CPUs .........

I. Using start_new_thread () to implement threads is a relatively underlying implementation method. All threads share their global data. To achieve synchronization, the module also provides a simple lock mechanism.

_ Thread. start_new_thread (function, args [, kwargs])
Start a new process and return its identifier. the parameters required for a function to be executed by a thread are provided by args (which must be a tuple), and the optional parameter kwargs can also be used to provide a dictionary composed of keyword parameters. When the function returns, the started thread also stops exiting. If an unhandled exception exists in the function, the thread stops and exits after the stack trace is printed (other threads continue to execute ).


The thread identifier is a non-zero integer and does not directly mean it. It can be used as a key to retrieve the thread from a special dictionary composed of a thread. It can also be used as a _ thread. get_ident () is obtained. After the thread exits, the identifier is recycled by the system. During thread execution, you can call _ thread. exit () to terminate the execution of this thread.

Java code
  1. Import _ thread
  2. Import time
  3. Def threadFunction (count ):
  4. For I in range (count ):
  5. Print ('print % d' % of process id % d (_ thread. get_ident (), I ))
  6. I-= 1
  7. Time. sleep (0.1)
  8. Def begin ():
  9. Ident1 = _ thread. start_new_thread (threadFunction, (100 ,))
  10. Print ('process with startup identifier % d' % (ident1 ,))
  11. Ident2 = _ thread. start_new_thread (threadFunction, (100 ,))
  12. Print ('process with startup identifier % d' % (ident2 ,))
  13. If _ name _ = '_ main __':
  14. Begin ()




II. the Thread class is used to implement multithreading. This method is an advanced encapsulation of the _ thread module (if there is no _ thread, it is dummy_threading). In this way, we need to create a new class to inherit threading. thread, Which is rewritten like threading in java. thread run method. the start thread uses the start method of the thread. It will call the run method we have rewritten.

Java code
  1. Class MyThread (threading. Thread ):
  2. '''Only the _ init _ and run methods can be rewritten '''
  3. Def _ init _ (self, name ):
  4. Threading. Thread. _ init _ (self)
  5. Self. name = name
  6. Self. bool_stop = False
  7. Def run (self ):
  8. While not self. bool_stop:
  9. Print ('process % s, at % s' % (self. name, time. asctime ()))
  10. Time. sleep (1)
  11. Def stop (self ):
  12. Self. bool_stop = True
  13. If _ name _ = '_ main __':
  14. Th1 = MyThread ('one ')
  15. YY = MyThread ('two ')
  16. Th1.start ()
  17. Th2.start ()




The Thread class also defines the following common methods and attributes:

Thread. getName () \ Thread. setName ()
The old method is used to obtain and set the Thread name. We recommend that you use Thread. name instead.
Thread. ident
Obtains the identifier of a thread. It is valid only after the start () method is called, otherwise None is returned.
Thread. is_alive ()
Determine whether the thread is activated.
Thread. join ([timeout])
Calling Thread. join will block the main Thread until the call Thread ends or times out. The timeout parameter is a numeric value that indicates the timeout time. If this parameter is not provided, the main thread will be blocked until the end of the called thread.



Locks in Python 

First use the Lock of the _ thread module to implement producer consumer issues. The Lock object is a low-level thread control tool provided by Python. It is very easy to use. You only need the following three statements:

_ Thread. allocate_lock () returns a new Lock object, that is, a new Lock.
Lock. acquire () is equivalent to the P operation, get a lock,
Lock. release () is equivalent to the V operation, releasing a lock


The Code is as follows:

Java code
  1. Import _ thread, time, random
  2. Dish = 0
  3. Lock = _ thread. allocate_lock ()
  4. Def producerFunction ():
  5. '''If the sieve is larger than 0.2, add an apple to the plate '''
  6. Global lock, dish
  7. While True:
  8. If (random. random ()> 0.1 ):
  9. Lock. acquire ()
  10. If dish <100:
  11. Dish + = 1
  12. Print ('the producer adds an apple, and now there are % d Apple '% (dish ,))
  13. Lock. release ()
  14. Time. sleep (random. random () * 3)
  15. Def consumerFunction ():
  16. '''If the sieve is larger than 0.5, take an apple from the plate '''
  17. Global lock, dish
  18. While True:
  19. If (random. random ()> 0.9 ):
  20. Lock. acquire ()
  21. If dish> 0:
  22. Dish-= 1
  23. Print ('consumer takes an apple now, with % d Apple '% (dish ,))
  24. Lock. release ()
  25. Time. sleep (random. random () * 3)
  26. Def begin ():
  27. Ident1 = _ thread. start_new_thread (producerFunction ,())
  28. Ident2 = _ thread. start_new_thread (consumerFunction ,())
  29. If _ name _ = '_ main __':
  30. Begin ()


Another advanced Lock is the RLock. The RLock object maintains a Lock object, which is a reentrant object. For the Lock object, if a thread performs the acquire operation twice in a row, the second acquire will suspend the thread because there is no release after the first acquire operation. This will cause the Lock Object to never be release and cause a thread deadlock. The RLock object allows a thread to perform the acquire operation multiple times because it maintains the number of acquire threads through a counter variable. Each acquire operation must have a release operation. After all the release operations are completed, other threads can apply for the RLock object.


The threading module also provides and encapsulates locks and provides more advanced Synchronization Methods (which can be understood as more advanced locks), including threading. event and threading. condition, where threading. event provides a simple synchronization method: one process marks the event, and other processes are waiting. You only need to use the following methods:

Event. wait ([timeout])
Blocks the thread 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 identification number to true.
Event. clear ()
Set as identifier False



Threading. Condition can understand Condiftion as a high level of redundancy. It provides more advanced functions than Lock and RLock, allowing us to control complicated thread synchronization problems. Threadiong. Condition maintains a wretched object internally (RLock by default). It can be passed as a parameter when a Condigtion object is created. Condition also provides the acquire and release methods, which have the same meaning as the wretched acquire and release methods. In fact, it is just a simple method to call the corresponding methods of the internal wretched objects. Condition also provides the following methods (Note: These methods can be called only after being occupied by the cumbersome (acquire); otherwise, a RuntimeError exception will be reported .) :

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. Condition y ():
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.

Why can't the following multi-threaded python code run? The function has no lock attribute.

The following code can be used, because your lock is a variable and a function, which may conflict. In addition, you cannot test the lock attribute in this experiment, because your call is actually sequential.

Import string, socket, time, OS, sys, threading
Num = 0
Class xThread ():
Def _ init _ (self ):
Self. _ lock = threading. Lock ()
Def lock (self ):
Self. _ lock. acquire ()
Def unlock (self ):
Self. _ lock. release ()

Def p2 ():
Global num
Thread. lock ()
Num + = 1
Print (a + str (num ))
Time. sleep (0.1)
Thread. unlock ()

Def p1 ():
For I in range (3 ):
Info = 'this % s thread' % (I)
P2 (info)

Def main ():
For I in range (3 ):
P1 (I)

If _ name _ = '_ main __':
Thread = xThread ()
Main ()

Python multithreading does not support viewing the number of locks

Add a variable to the lock to save the number of layers of the lock. Each acquire will add 1, and each release will be reduced by 1, so no additional function is needed.

Related Article

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.