Example of the Python threading module thread lock

Source: Internet
Author: User
Tags sleep sleep function


Python threading modules have two types of locks: mutual-exclusion locks (threading). Lock) and reusable locks (threading. Rlock). The use of the two is basically the same, as follows:

Lock = Threading. Lock ()
Lock.acquire ()
DoSomething ...
Lock.release ()
The use of Rlock is to modify Threading.lock () to threading. Rlock (). Easy to understand, first come to paragraph code:

[Root@361way lock]# Cat lock1.py
#!/usr/bin/env python
# Coding=utf-8
Import Threading # Importing Threading Module
Import time # Importing Time module
Class Mythread (threading. Thread): # Creating classes by inheriting
def __init__ (Self,threadname): # initialization method
# Call the initialization method of the parent class
Threading. thread.__init__ (self,name = threadname)
def run (self): # overloaded Run method
Global X # uses global to indicate that x is a global variable
For I in range (3):
x = x + 1
Time.sleep (5) # calls the Sleep function, leaving the thread dormant for 5 seconds
Print X
TL = [] # definition List
For I in range (10):
t = mythread (str (i)) # class instantiation
Tl.append (t) # Add a Class object to the list
X=0 # assigns X to 0
For I in TL:
I.start ()
The results of this implementation are different from those thought, and the results are as follows:

[Root@361way lock]# python lock1.py
30
30
30
30
30
30
30
30
30
30
Why are the results all 30? The key is global row and Time.sleep line.

1, since x is a global variable, the value of x after each cycle is the result value after execution;

2, because the code is multi-threaded operation, so in the sleep waiting, the previous execution of the thread will wait, and the follow-up process in the waiting 5 seconds This time also completed, waiting for print. Also because of the global principle, X is re bin value. So the printed results are all 30;

3, easy to understand, you can try to sleep and other annotations, you look at the results, you will find that there are different.

In practical applications, such as crawlers, there is a situation similar to sleep waiting. Before and after the call has the order or print output, there will be concurrent competition, resulting in the result or output disorder. The concept of the lock is introduced here, and the code above is modified as follows:

[Root@361way lock]# Cat lock2.py
#!/usr/bin/env python
# Coding=utf-8
Import Threading # Importing Threading Module
Import time # Importing Time module
Class Mythread (threading. Thread): # Creating classes by inheriting
def __init__ (Self,threadname): # initialization method
Threading. thread.__init__ (self,name = threadname)
def run (self): # overloaded Run method
Global X # uses global to indicate that x is a global variable
Lock.acquire () # calls the lock's Acquire method
For I in range (3):
x = x + 1
Time.sleep (5) # calls the Sleep function, leaving the thread dormant for 5 seconds
Print X
Lock.release () # calls the lock's release method
Lock = Threading. Lock () # class instantiation
TL = [] # definition List
For I in range (10):
t = mythread (str (i)) # class instantiation
Tl.append (t) # Add a Class object to the list
X=0 # assigns X to 0
For I in TL:
I.start () # Run threads sequentially
The results of the implementation are as follows:

[Root@361way lock]# python lock2.py
3
6
9
12
15
18
21st
24
27
30
The result of locking will be blocked, and will result in large unlock. According to the order of the concurrent multithreading in sequential output, if the following thread execution too fast, you need to wait for the previous process to end its ability to finish--writing seems a bit like the concept of a queue, but in many of the scenes with locks can indeed be resolved through the queue.

Finally, introduce an example, in the stock quantitative Analysis (ii) PE and Circulation market value article, introduced how to collect the two index, and output according to the results, but in the output when there will be output disorder, as follows:

Threading-lock

such as 600131 and 000708 of the stockid on the output to the same line, although through multithreading so that the implementation of a lot faster, but this is very beautiful, and not easy to follow-up processing.

1, output competition disorder code

#!/usr/bin/python
#coding =utf-8
# 1, PE in the 0~20 between the enterprise
# 2, enterprises with less than 5 billion of the circulating capital
Import Urllib2
Import time
Import JSON
From threading Import Thread
def get_pe (Stockid):
Try
url = ' http://d.10jqka.com.cn/v2/realhead/hs_%s/last.js '% stockid
Send_headers = {
' Host ': ' d.10jqka.com.cn ',
' Referer ': ' http://stock.10jqka.com.cn/',
' Accept ': ' Application/json, Text/javascript, */*; q=0.01 ',
' Connection ': ' Keep-alive ',
' User-agent ': ' mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/38.0.2125.122 safari/537.36 ',
' X-forwarded-for ': ' 124.160.148.178 ',
' X-requested-with ': ' XMLHttpRequest '
}
req = Urllib2. Request (Url,headers=send_headers)
F = Urllib2.urlopen (req)
data = F.read (). Split (' Items ': ', 1) [1]
data = Data.split ('} ') ', 1 [0]
J_data = json.loads (data)
#J_data = Json.dumps (data,indent=4,encoding= ' utf-8 ')
Stockpe = j_data[' 2034120 ']
StockName = j_data[' name ']
Sumvalue = j_data[' 3475914 ']
Currentprice = j_data[' 10 ']
#print Stockid,stockname,stockpe
Return Stockname,stockpe,sumvalue,currentprice
Except Urllib2. Httperror, E:
#return Stockid, ' Get happed httperror '
Return E.code
def cond (Stockid,pe,asset):
PE = INT (PE)
Asset = Int (asset)
Try
Stockname,stockpe,sumvalue,currentprice = Get_pe (stockid)
If Sumvalue:
Billvalue = Round (float (sumvalue)/1000/1000/100)
Else
Billvalue = 0
If Stockpe:
If float (STOCKPE) > 0 and float (STOCKPE) < PE and Billvalue < asset:
Print Stockid,stockname,currentprice,stockpe,billvalue
#else:
# Print Stockid
Except TypeError, E:
Print Stockid, ' get error '
if __name__ = = ' __main__ ':
Threads = []
print ' Stockid stockname currentprice stockpe billvalue '
Stockids = [Line.strip () for line in open ("Stock_exp.txt", ' R ')]
Nloops = Range (len (stockids))
For Stockid in Stockids:
t = Thread (Target=cond, args= (stockid,28,80))
Threads.append (t)
For I in Nloops:
Threads[i].start ()
For I in Nloops:
Threads[i].join ()

2, after the lock code

#!/usr/bin/python
#coding =utf-8
# 1, PE in the 0~20 between the enterprise
# 2, enterprises with less than 5 billion of the circulating capital
Import threading
Import Urllib2
Import time
Import JSON
Lock = Threading. Lock ()
def get_pe (Stockid):
Try
url = ' http://d.10jqka.com.cn/v2/realhead/hs_%s/last.js '% stockid
Send_headers = {
' Host ': ' d.10jqka.com.cn ',
' Referer ': ' http://stock.10jqka.com.cn/',
' Accept ': ' Application/json, Text/javascript, */*; q=0.01 ',
' Connection ': ' Keep-alive ',
' User-agent ': ' mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/38.0.2125.122 safari/537.36 ',
' X-forwarded-for ': ' 124.160.148.178 ',
' X-requested-with ': ' XMLHttpRequest '
}
req = Urllib2. Request (Url,headers=send_headers)
F = Urllib2.urlopen (req)
data = F.read (). Split (' Items ': ', 1) [1]
data = Data.split ('} ') ', 1 [0]
J_data = json.loads (data)
#J_data = Json.dumps (data,indent=4,encoding= ' utf-8 ')
Stockpe = j_data[' 2034120 ']
StockName = j_data[' name ']
Sumvalue = j_data[' 3475914 ']
Currentprice = j_data[' 10 ']
#print Stockid,stockname,stockpe
Return Stockname,stockpe,sumvalue,currentprice
Except Urllib2. Httperror, E:
#return Stockid, ' Get happed httperror '
Return E.code
def cond (Stockid,pe,asset):
PE = INT (PE)
Asset = Int (asset)
Try
Stockname,stockpe,sumvalue,currentprice = Get_pe (stockid)
If Sumvalue:
Billvalue = Round (float (sumvalue)/1000/1000/100)
Else
Billvalue = 0
If Stockpe:
If float (STOCKPE) > 0 and float (STOCKPE) < PE and Billvalue < asset:
Lock.acquire ()
Print Stockid,stockname,currentprice,stockpe,billvalue
Lock.release ()
#else:
# Print Stockid
Except TypeError, E:
Print Stockid, ' get error '
if __name__ = = ' __main__ ':
Threads = []
print ' Stockid stockname currentprice stockpe billvalue '
Stockids = [Line.strip () for line in open ("Stock_exp.txt", ' R ')]
For Stockid in Stockids:
t = Threading. Thread (Target=cond, args= (stockid,25,50))
Threads.append (t)
T.start ()

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.