Python uses lock to access Shared variable instance parsing, python variable

Source: Internet
Author: User

Python uses lock to access Shared variable instance parsing, python variable

This article focuses on the use of lock access Shared variables in python. The specific introduction and implementation are as follows.

When multiple threads access a variable at the same time during multi-thread programming in python, the variable data may be damaged. The threading module in pyhon provides the lock Object, the acquire method in lock is used to obtain a lock, while release is used to release a lock. When a thread gets the lock, it gets the access to the shared variable and becomes blocked. If other threads apply to access the variable, they must wait until the thread calls the release method to release the lock. The following is an example of using locks in python:

#!/usr/bin/env python import threading,time q=threading.Lock()  #create a lock object def mythread():   global a   q.acquire()   #acquire the lock   a=threading.currentThread().getName()   print "a is modified by",a   q.release()   #release the lock    for i in range(1,4):   t=threading.Thread(target=mythread,name="Thread %d"%i)   t.start() 

If a thread wants to obtain resource access multiple timesacquire(), Will cause a deadlock, because the resources applied for the first time have not yet been released, the second application was made. The threading module in python provides reentrant lock RLock and RLock provides counters. When a thread applies for a resource, the counter will add 1. If the resource is released, the counter will be reduced by 1. in this way, a thread can request the same resource multiple times. Only when all requests are released can other threads obtain the lock. The above code can be modified in a simple way:

#!/usr/bin/env python import threading,time q=threading.RLock()    #create a lock object def mythread():   global a   q.acquire()    #acquire the lock   a=threading.currentThread().getName()   print "a is modified by",a   q.acquire()   a=threading.currentThread().getName()   print "a is modified by %s the second time"% a   q.release()      q.release()    #release the lock for i in range(1,4):   t=threading.Thread(target=mythread,name="Thread %d"%i)   t.start() 
Summary

The above is all the content of this article on python's use of lock access to shared variable instance parsing. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.