How to build a Python thread lock Module

Source: Internet
Author: User

The Python thread lock requires us to constantly learn related technologies. In fact, sometimes we can also find solutions to problems during continuous learning. Multithreading is an important aspect in programming, especially in the Deamon program of the server. Regardless of the system, the thread scheduling overhead is much faster than the traditional process.

Python supports multiple threads. Allows you to quickly create threads, mutex locks, semaphores, and other elements, and supports synchronization and mutex between read and write threads. In the US, Python runs on a Python virtual machine. The multi-thread created may be a virtual thread and needs to be polling and scheduled by the Python virtual machine, which greatly reduces the availability of Python multi-thread. We hope that later versions of Python can solve this problem and maximize the efficiency of multiple CPUs.
Some friends on the Internet say there are two ways to achieve the benefits of real multi-CPU:

1. You can create multiple processes instead of threads. The number of processes is the same as that of the cpu.

2. Using Jython or IronPython, you can get real multithreading.

Create a Python thread lock

Use the Thread class of the threading module. The class interface is as follows:

 
 
  1. class Thread( group=None, target=None, name=None, 
    args=(), kwargs={}) 

Target and args. target are target functions that require Sub-threads to run, and args are function parameters that are passed in the form of tuple.
The following code creates a subthread pointing to the worker Function

 
 
  1. def worker(a_tid,a_account):  
  2. ...  
  3. th = threading.Thread(target=worker,args=(i,acc) ) ; 

Start the Python thread lock

 
 
  1. th.start() 

Wait until the Thread returns threading. Thread. join (th) or th. join ()

If you can divide the data to be processed and there is no need to communicate between threads, You can compile your multi-threaded program in the following way: Create = run = recycle. However, if the threads need to access a common object, the mutex lock or semaphore must be introduced to access resources mutex.

The following describes how to create a mutex lock and create a lock.

 
 
  1. g_mutex = threading.Lock()  
  2. .... 

Use Python thread lock

 
 
  1. For ...:
  2. # Lock, mutual access exclusive from the next code to before release
  3. G_mutex.acquire ()
  4. A_account.deposite (1)
  5. # Release
  6. G_mutex.release ()

The above is an introduction to the Python thread lock system. I hope you will gain some benefits.

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.