{"id":1578,"date":"2023-03-25T11:33:17","date_gmt":"2023-03-25T03:33:17","guid":{"rendered":""},"modified":"2023-03-25T11:33:17","modified_gmt":"2023-03-25T03:33:17","slug":"Python\u7ebf\u7a0b\u540c\u6b65","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1578.html","title":{"rendered":"Python\u7ebf\u7a0b\u540c\u6b65"},"content":{"rendered":"
\n
Python\u7ebf\u7a0b\u540c\u6b65\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n \u50f5\u5c40<\/span> \u503c\u8bbe\u7f6e\u4e3aTrue <\/b>-\u5982\u679c\u4f7f\u7528True\uff08\u9ed8\u8ba4\u53c2\u6570\uff09\u8c03\u7528acquire()\u65b9\u6cd5\uff0c\u5219\u7ebf\u7a0b\u6267\u884c\u5c06\u88ab\u963b\u585e\uff0c\u76f4\u5230\u9501\u88ab\u89e3\u9501\u3002<\/span> \u5982\u679c\u9501\u88ab\u9501\u5b9a\uff0c\u5219 release()<\/b>\u65b9\u6cd5\u5c06\u5176\u89e3\u9501\u3002\u5b83\u7684\u5de5\u4f5c\u662f\uff0c\u5982\u679c\u4e00\u4e2a\u4ee5\u4e0a\u7684\u7ebf\u7a0b\u88ab\u963b\u585e\u5e76\u7b49\u5f85\u9501\u88ab\u89e3\u9501\uff0c\u5219\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u7ee7\u7eed\u8fd0\u884c\u3002<\/span> <\/p>\n
\u95ee\u9898\u51fa\u73b0\u5728\u7ebf\u7a0b\u540c\u6b65\u4e2d<\/h2>\n
\n \u79cd\u65cf\u72b6\u51b5<\/span> <\/p>\n\u79cd\u65cf\u72b6\u51b5<\/h3>\n
\u793a\u4f8b<\/h3>\n
import <\/span>threading
<\/span><\/code><\/pre>\n<\/p><\/div>\n x = 0
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n increment_global()<\/b>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u6b64\u5168\u5c40\u51fd\u6570x\u4e2d\u8fdb\u884c1\u7684\u9012\u589e-\n <\/div>\n def <\/span>increment_global<\/span>():
global<\/span> x
x += 1
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n taskofThread()<\/b>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728\u6307\u5b9a\u7684\u6b21\u6570\u5185\u8c03\u7528increment_global()\u51fd\u6570\uff1b\u5bf9\u4e8e\u6211\u4eec\u7684\u793a\u4f8b\uff0c\u5b83\u662f50000\u6b21-\n <\/div>\n def <\/span>taskofThread():
for <\/span><\/span>_ in <\/span>range(50000):
increment_global<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n def <\/span>main():
global<\/span> x
x = 0
t1 = threading.Thread<\/span>(target= taskofThread)
t2 = threading.Thread<\/span>(target= taskofThread)
t1.start<\/span>()
t2.start<\/span>()
t1.join<\/span>()
t2.join<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n if <\/span>__name__ == \"__main__\"<\/span>:
for <\/span><\/span>i in <\/span>range(5):
main()
print(\"x = {1} after Iteration {0}\"<\/span>.format<\/span>(i,x))
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
x = 100000 after Iteration 0
x = 54034 after Iteration 1
x = 80230 after Iteration 2
x = 93602 after Iteration 3
x = 93289 after Iteration 4
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u4f7f\u7528\u9501\u5b9a\u5904\u7406\u7ade\u4e89\u6761\u4ef6<\/h2>\n
\n \n
\n Lock <\/b>\u7c7b\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u65b9\u6cd5\uff0c\u501f\u52a9\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5904\u7406\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u7684\u7ade\u4e89\u6761\u4ef6\u3002\u65b9\u6cd5\u5982\u4e0b\u6240\u8ff0-\n <\/div>\n acquire()\u65b9\u6cd5<\/h3>\n
\n \u5c06\u503c\u8bbe\u7f6e\u4e3aFalse <\/b>-\u5982\u679c\u4f7f\u7528False\uff08\u4e0d\u662f\u9ed8\u8ba4\u53c2\u6570\uff09\u8c03\u7528acquire()\u65b9\u6cd5\uff0c\u5219\u5728\u5c06\u5176\u8bbe\u7f6e\u4e3atrue\u4e4b\u524d\uff0c\u4e0d\u4f1a\u963b\u585e\u7ebf\u7a0b\u6267\u884c\u3002\u76f4\u5230\u88ab\u9501\u5b9a\u3002<\/span> <\/p>\n release()\u65b9\u6cd5<\/h3>\n
\n \u5982\u679c\u9501\u5b9a\u5df2\u89e3\u9501\uff0c\u5b83\u5c06\u5f15\u53d1 ThreadError <\/b>\u3002<\/span> <\/p>\n\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>threading
x = 0
def <\/span>increment_global<\/span>():
global<\/span> x
x += 1
def <\/span>taskofThread(lock):
for <\/span><\/span>_ in <\/span>range(50000):
lock.acquire<\/span>()
increment_global<\/span>()
lock.release<\/span>()
def <\/span>main():
global<\/span> x
x = 0
lock = threading.Lock<\/span>()
t1 = threading.Thread<\/span>(target = taskofThread, args = (lock,))
t2 = threading.Thread<\/span>(target = taskofThread, args = (lock,))
t1.start<\/span>()
t2.start<\/span>()
t1.join<\/span>()
t2.join<\/span>()
if <\/span>__name__ == \"__main__\"<\/span>:
for <\/span><\/span>i in <\/span>range(5):
main()
print(\"x = {1} after Iteration {0}\"<\/span>.format<\/span>(i,x))
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
x = 100000 after Iteration 0
x = 100000 after Iteration 1
x = 100000 after Iteration 2
x = 100000 after Iteration 3
x = 100000 after Iteration 4
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u50f5\u5c40-\u9910\u996e\u54f2\u5b66\u5bb6\u7684\u95ee\u9898<\/h2>\n
\u5e76\u53d1\u7cfb\u7edf\u4e2d\u7684\u6b7b\u9501<\/h3>\n
Python\u7a0b\u5e8f\u89e3\u51b3\u65b9\u6848<\/h3>\n
\u793a\u4f8b<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
import <\/span>threading
import <\/span>random
import <\/span>time
class <\/span>DiningPhilosopher(threading.Thread):
running = True<\/span>
def <\/span>__init__(self, xname, Leftfork, Rightfork):
threading.Thread.__init__<\/span>(self)
self.name = xname
self.Leftfork = Leftfork
self.Rightfork = Rightfork
def <\/span>run(self):
while(self.running):
time.sleep<\/span>( random.uniform<\/span>(3,13))
print <\/span>('%s is <\/span>hungry.'<\/span> % self.name)
self.dine<\/span>()
def <\/span>dine(self):
fork1, fork2 = self.Leftfork, self.Rightfork
while <\/span>self.running:
fork1.acquire<\/span>(True)
locked = fork2.acquire<\/span>(False<\/span>)
if <\/span>locked: break<\/span>
fork1.release<\/span>()
print <\/span>('%s swaps forks'<\/span> % self.name)
fork1, fork2 = fork2, fork1
else:<\/span>
return
self.dining<\/span>()
fork2.release<\/span>()
fork1.release<\/span>()
def <\/span>dining(self):
print <\/span>('%s starts eating '<\/span>% self.name)
time.sleep<\/span>(random.uniform<\/span>(1,10))
print <\/span>('%s finishes eating and <\/span>now thinking.'<\/span> % self.name)
def <\/span>Dining_Philosophers():
forks = [threading.Lock<\/span>() for <\/span><\/span>n in <\/span>range(5)]
philosopherNames = ('1st'<\/span>,'2nd'<\/span>,'3rd'<\/span>,'4th'<\/span>, '5th'<\/span>)
philosophers= [DiningPhilosopher(philosopherNames[i], forks[i%5], forks[(i+1)%5]) \\
for <\/span><\/span>i in <\/span>range(5)]
random.seed<\/span>()
DiningPhilosopher.running = True<\/span>
for <\/span><\/span>p in <\/span>philosophers: p.start<\/span>()
time.sleep<\/span>(30)
DiningPhilosopher.running = False<\/span>
print <\/span>(\" It is <\/span>finishing.\"<\/span>)
Dining_Philosophers()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \n
\n Lock <\/b>\u7c7b\u7684
\n acquire()<\/b>\u548c
\n release()<\/b>\u65b9\u6cd5>\u6a21\u5757\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u4e0b\u9762\u7684\u8f93\u51fa\u4e2d\u770b\u5230\u89e3\u51b3\u65b9\u6848-\n <\/div>\n\u8f93\u51fa<\/h3>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Bianchenghao6<\/span>
# Author by : bianchenghao6.com<\/span>
# Date : 2020-08-22<\/span>
4th is <\/span>hungry.
4th starts eating
1st is <\/span>hungry.
1st starts eating
2nd is <\/span>hungry.
5th is <\/span>hungry.
3rd is <\/span>hungry.
1st finishes eating and <\/span>now thinking.3rd swaps forks
2nd starts eating
4th finishes eating and <\/span>now thinking.
3rd swaps forks5th starts eating
5th finishes eating and <\/span>now thinking.
4th is <\/span>hungry.
4th starts eating
2nd finishes eating and <\/span>now thinking.
3rd swaps forks
1st is <\/span>hungry.
1st starts eating
4th finishes eating and <\/span>now thinking.