Python澶氱嚎绋嬩箣姝婚攣
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python澶氱嚎绋嬩箣姝婚攣相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/hex' title='hex'>hex
鍗犵敤 婕旂ず 鍚戦噺 init open http 寰幆 鍚屾
1.浠€涔堟槸姝婚攣锛?/h3>
姝婚攣鏄敱浜庝袱涓垨浠ヤ笂鐨勭嚎绋嬩簰鐩告寔鏈夊鏂归渶瑕佺殑璧勬簮锛屼笖閮戒笉閲婃斁鍗犳湁鐨勮祫婧愶紝瀵艰嚧杩欎簺绾跨▼澶勪簬绛夊緟鐘舵€侊紝绋嬪簭鏃犳硶鎵ц銆?/p>
2.浜х敓姝婚攣鐨勫洓涓繀瑕佹潯浠?/h3>
銆€銆€銆€1.浜掓枼鎬э細绾跨▼瀵硅祫婧愮殑鍗犳湁鏄帓浠栨€х殑锛屼竴涓祫婧愬彧鑳借涓€涓嚎绋嬪崰鏈夛紝鐩村埌閲婃斁銆?/p>
銆€銆€銆€2.璇锋眰鍜屼繚鎸佹潯浠讹細涓€涓嚎绋嬪璇锋眰琚崰鏈夎祫婧愬彂鐢熼樆濉炴椂锛屽宸茬粡鑾峰緱鐨勮祫婧愪笉閲婃斁銆?/p>
銆€銆€銆€3.涓嶅墺澶猴細涓€涓嚎绋嬪湪閲婃斁璧勬簮涔嬪墠锛屽叾浠栫殑绾跨▼鏃犳硶鍓ュず鍗犵敤銆?/p>
銆€銆€銆€4.寰幆绛夊緟锛氬彂鐢熸閿佹椂锛岀嚎绋嬭繘鍏ユ寰幆锛屾案涔呴樆濉炪€?/p>
3.浜х敓姝婚攣鐨勫師鍥?/h3>
鍦ㄥ绾跨▼鐨勫満鏅紝姣斿绾跨▼A鎸佹湁鐙崰閿佽祫婧恆锛屽苟灏濊瘯鍘昏幏鍙栫嫭鍗犻攣璧勬簮b鍚屾椂锛岀嚎绋婤鎸佹湁鐙崰閿佽祫婧恇锛屽苟灏濊瘯鍘昏幏鍙栫嫭鍗犻攣璧勬簮a銆?br />杩欐牱绾跨▼A鍜岀嚎绋婤鐩镐簰鎸佹湁瀵规柟闇€瑕佺殑閿侊紝浠庤€屽彂鐢熼樆濉烇紝鏈€缁堝彉涓烘閿併€?/p>
閫犳垚姝婚攣鐨勫師鍥犲彲浠ユ鎷垚涓夊彞璇濓細
1.涓嶅悓绾跨▼鍚屾椂鍗犵敤鑷繁閿佽祫婧?br />2.杩欎簺绾跨▼閮介渶瑕佸鏂圭殑閿佽祫婧?br />3.杩欎簺绾跨▼閮戒笉鏀惧純鑷繁鎷ユ湁鐨勮祫婧?/p>
绾跨▼A鎸佹湁閿佽祫婧恆鐨勫悓鏃讹紝绾跨▼B涔熸寔鏈変簡閿佽祫婧恇銆?br />绾跨▼A鎯宠缁х画鎵ц闇€瑕侀攣璧勬簮b锛岀嚎绋婤鎯宠缁х画鎵ц闇€瑕侀攣璧勬簮a
绾跨▼A涓嶉噴鏀鹃攣璧勬簮a锛岀嚎绋婤涓嶉噴鏀鹃攣璧勬簮b
绾跨▼A绾跨▼B閮介渶瑕乤锛宐涓ゆ妸閿侊紝濡傛灉鎴戜滑鍔犻攣鐨勯『搴忎竴鑷达紙绾跨▼A鍏堟嬁a鍔犻攣锛屽啀鎷縝鍔犻攣锛屽啀瑙i攣b锛岀劧鍚庤В閿乤锛岀嚎绋婤鍚岀悊锛夛紝灏变笉浼氬嚭鐜版閿佺殑鎯呭喌銆?/p>
4.涓夌鍏稿瀷鐨勬閿?/h3>
甯歌鐨?绉嶆閿佺殑绫诲瀷:闈欐€佺殑閿侀『搴忔閿?/strong>锛?strong>鍔ㄦ€佺殑閿侀『搴忔閿?/strong>锛?strong>鍗忎綔瀵硅薄涔嬮棿鐨勬閿?/strong>銆?/p>
闈欐€佺殑閿侀『搴忔閿?/strong> a鍜宐涓や釜鏂规硶閮介渶瑕佽幏寰桝閿佸拰B閿併€備竴涓嚎绋嬫墽琛宎鏂规硶涓斿凡缁忚幏寰椾簡A閿侊紝鍦ㄧ瓑寰匓閿侊紱鍙︿竴涓嚎绋嬫墽琛屼簡b鏂规硶涓斿凡缁忚幏寰椾簡B閿侊紝鍦ㄧ瓑寰匒閿併€傝繖绉嶇姸鎬侊紝灏辨槸鍙戠敓浜嗛潤鎬佺殑閿侀『搴忔閿併€?/p>
闈欐€佹槸鎸囷紝鍦ㄧ▼搴忎腑锛屽浜庢煇涓攣鏉ヨ鍔犻攣鍜岃В閿佺殑浣嶇疆鏄笉鍙樼殑銆?/p>
鎴戜滑鐢≒ython鐩磋鐨勬紨绀轰竴涓嬮潤鎬佺殑閿侀『搴忔閿併€?br />鍋囪閾惰绯荤粺涓紝鐢ㄦ埛a璇曞浘杞处100鍧楃粰鐢ㄦ埛b锛屼笌姝ゅ悓鏃剁敤鎴穊璇曞浘杞处200鍧楃粰鐢ㄦ埛a锛屽垯鍙兘浜х敓姝婚攣銆?br />2涓嚎绋嬩簰鐩哥瓑寰呭鏂圭殑閿侊紝浜掔浉鍗犵敤鐫€璧勬簮涓嶉噴鏀俱€?/p>
鍔ㄦ€佺殑閿侀『搴忔閿?/strong> 鍗忎綔瀵硅薄涔嬮棿鐨勬閿?/strong> 濡傛灉鍦ㄦ寔鏈夐攣鏃惰皟鐢ㄦ煇涓閮ㄦ柟娉曪紝閭d箞灏嗗彲鑳藉嚭鐜版閿侀棶棰樸€傚湪杩欎釜澶栭儴鏂规硶涓彲鑳戒細鑾峰緱鍏朵粬閿侊紝鎴栬€呴樆濉炴椂闂磋繃闀匡紝瀵艰嚧鍏朵粬绾跨▼鏃犳硶鍙婃椂鑾峰緱褰撳墠琚寔鏈夌殑閿併€?/p>
涓轰簡閬垮厤杩欑鍗遍櫓鐨勬儏鍐靛彂鐢燂紝鎴戜滑浣跨敤寮€鏀捐皟鐢ㄣ€傚鏋滆皟鐢ㄦ煇涓閮ㄦ柟娉曟椂涓嶉渶瑕佹寔鏈夐攣锛屾垜浠О涔嬩负寮€鏀捐皟鐢ㄣ€?/p>
閬垮厤姝婚攣鍙互姒傛嫭鎴愪笁绉嶆柟娉曪細 閿侀『搴忔搷浣滅殑姝婚攣锛?/strong> 瑙e喅闈欐€佺殑閿侀『搴忔閿佺殑鏂规硶锛氭墍鏈夐渶瑕佸涓攣鐨勭嚎绋嬶紝閮借浠ョ浉鍚岀殑椤哄簭鏉ヨ幏寰楅攣銆?/p>
瑙e喅鍔ㄦ€佺殑閿侀『搴忔閿佺殑鏂规硶锛氭瘮杈冧紶鍏ラ攣瀵硅薄鐨勫搱甯屽€硷紝鏍规嵁鍝堝笇鍊肩殑澶у皬鏉ョ‘淇濇墍鏈夌殑绾跨▼閮戒互鐩稿悓鐨勯『搴忚幏寰楅攣 銆?/p>
python涓娇鐢ㄤ笂涓嬫枃绠$悊鍣ㄦ潵瑙e喅鍔ㄦ€佺殑閿侀『搴忔閿侀棶棰橈紝褰撶劧杩樻槸鍥哄畾閿佺殑椤哄簭鎿嶄綔锛?/strong>Python涓閿佺殑褰㈡垚绀轰緥鍙婃閿佹儏鍐电殑闃叉 瑙e喅鏂规鏄负绋嬪簭涓殑姣忎竴涓攣鍒嗛厤涓€涓敮涓€鐨刬d锛岀劧鍚庡彧鍏佽鎸夌収鍗囧簭瑙勫垯鏉ヤ娇鐢ㄥ涓攣锛岃繖涓鍒欎娇鐢ㄤ笂涓嬫枃绠$悊鍣?鏄潪甯稿鏄撳疄鐜扮殑锛岀ず渚嬪涓嬶細 濡備綍浣跨敤杩欎釜涓婁笅鏂囩鐞嗗櫒鍛紵浣犲彲浠ユ寜鐓ф甯搁€斿緞鍒涘缓涓€涓攣瀵硅薄锛屼絾涓嶈鏄崟涓攣杩樻槸澶氫釜閿佷腑閮戒娇鐢?acquire() 鍑芥暟鏉ョ敵璇烽攣锛?绀轰緥濡備笅锛?/p>
濡傛灉浣犳墽琛岃繖娈典唬鐮侊紝浣犱細鍙戠幇瀹冨嵆浣垮湪涓嶅悓鐨勫嚱鏁颁腑浠ヤ笉鍚岀殑椤哄簭鑾峰彇閿佷篃娌℃湁鍙戠敓姝婚攣銆?鍏跺叧閿湪浜庯紝鍦ㄧ涓€娈典唬鐮佷腑锛屾垜浠杩欎簺閿佽繘琛屼簡鎺掑簭銆傞€氳繃鎺掑簭锛屼娇寰椾笉绠$敤鎴蜂互浠€涔堟牱鐨勯『搴忔潵璇锋眰閿侊紝杩欎簺閿侀兘浼氭寜鐓у浐瀹氱殑椤哄簭琚幏鍙栥€?/p>
寮€鏀捐皟鐢?閽堝瀵硅薄涔嬮棿鍗忎綔閫犳垚鐨勬閿?锛?/strong> 瑙e喅鍗忎綔瀵硅薄涔嬮棿鍙戠敓鐨勬閿侊細闇€瑕佷娇鐢ㄥ紑鏀捐皟鐢紝鍗抽伩鍏嶅湪鎸佹湁閿佺殑鎯呭喌涓嬭皟鐢ㄥ閮ㄧ殑鏂规硶锛?strong>灏辨槸灏介噺灏嗛攣鐨勮寖鍥寸缉灏忥紝灏嗗悓姝ヤ唬鐮佸潡浠呯敤浜庝繚鎶ら偅浜涜璁″叡浜姸鎬佺殑鎿嶄綔銆?/p>
浣跨敤瀹氭椂閿?锛?/strong> 鍔犱笂涓€涓秴鏃舵椂闂达紝鑻ヤ竴涓嚎绋嬫病鏈夊湪缁欏畾鐨勬椂闄愬唴鎴愬姛鑾峰緱鎵€鏈夐渶瑕佺殑閿侊紝鍒欎細杩涜鍥為€€骞堕噴鏀炬墍鏈夊凡缁忚幏寰楃殑閿侊紝鐒跺悗绛夊緟涓€娈甸殢鏈虹殑鏃堕棿鍐嶉噸璇曘€?/p>
浣嗘槸濡傛灉鏈夐潪甯稿鐨勭嚎绋嬪悓涓€鏃堕棿鍘荤珵浜夊悓涓€鎵硅祫婧愶紝灏辩畻鏈夎秴鏃跺拰鍥為€€鏈哄埗锛岃繕鏄彲鑳戒細瀵艰嚧杩欎簺绾跨▼閲嶅鍦板皾璇曚絾鍗村缁堝緱涓嶅埌閿併€?/p>
姝婚攣妫€娴嬶細姝婚攣妫€娴嬪嵆姣忓綋涓€涓嚎绋嬭幏寰椾簡閿侊紝浼氬湪绾跨▼鍜岄攣鐩稿叧鐨勬暟鎹粨鏋勪腑锛?map 銆?graph 绛夛級灏嗗叾璁颁笅銆傞櫎姝や箣澶栵紝姣忓綋鏈夌嚎绋嬭姹傞攣锛屼篃闇€瑕佽褰曞湪杩欎釜鏁版嵁缁撴瀯涓€傛閿佹娴嬫槸涓€涓洿濂界殑姝婚攣棰勯槻鏈哄埗锛屽畠涓昏鏄拡瀵归偅浜涗笉鍙兘瀹炵幇鎸夊簭鍔犻攣骞朵笖閿佽秴鏃朵篃涓嶅彲琛岀殑鍦烘櫙銆?br />鍏朵腑锛屾閿佹娴嬫渶鍑哄悕鐨勭畻娉曟槸鐢辫壘鍏规牸·杩澃鏂壒鎷夊湪 1965 骞磋璁$殑閾惰瀹剁畻娉曪紝閫氳繃璁板綍绯荤粺涓殑璧勬簮鍚戦噺銆佹渶澶ч渶姹傜煩闃点€佸垎閰嶇煩闃点€侀渶姹傜煩闃碉紝浠ヤ繚璇佺郴缁熷彧鍦ㄥ畨鍏ㄧ姸鎬佷笅杩涜璧勬簮鍒嗛厤锛岀敱姝ゆ潵閬垮厤姝婚攣銆?/p>
以上是关于Python澶氱嚎绋嬩箣姝婚攣的主要内容,如果未能解决你的问题,请参考以下文章 mysql姝婚攣com.mysql.cj.jdbc.exception.MYSQLTransactionRollbackException Deadlock found when trying to # coding=utf-8
import time
import threading
class Account:
def __init__(self, _id, balance):
self.id = _id
self.balance = balance
def withdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
def transfera_b(_from, to, amount):
lock_a.acquire() # 閿佷綇鑷繁鐨勮处鎴?/span>
time.sleep(1) # 璁╀氦鏄撴椂闂村彉闀匡紝2涓氦鏄撶嚎绋嬫椂闂翠笂閲嶅彔锛屾湁瓒冲鏃堕棿鏉ヤ骇鐢熸閿?/span>
_from.withdraw(amount)
print(鈥?/span>wait for lock_b鈥?/span>)
lock_b.acquire() # 閿佷綇瀵规柟鐨勮处鎴?/span>
to.deposit(amount)
lock_b.release()
lock_a.release()
def transferb_a(_from, to, amount):
lock_b.acquire() # 閿佷綇鑷繁鐨勮处鎴?/span>
time.sleep(1) # 璁╀氦鏄撴椂闂村彉闀匡紝2涓氦鏄撶嚎绋嬫椂闂翠笂閲嶅彔锛屾湁瓒冲鏃堕棿鏉ヤ骇鐢熸閿?/span>
_from.withdraw(amount)
print(鈥?/span>wait for lock_a鈥?/span>)
lock_a.acquire() # 閿佷綇瀵规柟鐨勮处鎴?/span>
to.deposit(amount)
lock_a.release()
lock_b.release()
lock_a = threading.Lock()
lock_b = threading.Lock()
a = Account(鈥?/span>a鈥?/span>, 1000)
b = Account(鈥?/span>b鈥?/span>, 1000)
#a寰€b杞处100
t1 = threading.Thread(target=transfera_b, args=(a, b, 100))
t1.start()
#b寰€a杞处200
t2 = threading.Thread(target=transferb_a, args=(b, a, 200))
t2.start()
t1.join()
t2.join()
print("a鐨勮处鎴蜂綑棰濓細",a.balance)
print("b鐨勮处鎴蜂綑棰濓細",b.balance)
# coding=utf-8
import time
import threading
class Account:
def __init__(self, _id, balance):
self.id = _id
self.balance = balance
self.lock = threading.Lock()
def withdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
def transfer(_from,to, amount):
_from.lock.acquire() # 閿佷綇鑷繁鐨勮处鎴?/span>
time.sleep(1) # 璁╀氦鏄撴椂闂村彉闀匡紝2涓氦鏄撶嚎绋嬫椂闂翠笂閲嶅彔锛屾湁瓒冲鏃堕棿鏉ヤ骇鐢熸閿?/span>
_from.withdraw(amount)
print(鈥?/span>wait for lock鈥?/span>)
to.lock.acquire() # 閿佷綇瀵规柟鐨勮处鎴?/span>
to.deposit(amount)
to.lock.release()
_from.lock.release()
a = Account(鈥?/span>a鈥?/span>, 1000)
b = Account(鈥?/span>b鈥?/span>, 1000)
#a寰€b杞处100
t1 = threading.Thread(target=transfer, args=(a, b, 100))
t1.start()
#b寰€a杞处200
t2 = threading.Thread(target=transfer, args=(b, a, 200))
t2.start()
t1.join()
t2.join()
print("a鐨勮处鎴蜂綑棰濓細",a.balance)
print("b鐨勮处鎴蜂綑棰濓細",b.balance)
5.閬垮厤姝婚攣鐨勬柟娉曪紙閲嶇偣锛?/h3>
# coding=utf-8
import time
import threading
class Account:
def __init__(self, _id, balance):
self.id = _id
self.balance = balance
def withdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
def transfera_b(_from, to, amount):
lock_a.acquire() # 閿佷綇鑷繁鐨勮处鎴?/span>
time.sleep(1) # 璁╀氦鏄撴椂闂村彉闀匡紝2涓氦鏄撶嚎绋嬫椂闂翠笂閲嶅彔锛屾湁瓒冲鏃堕棿鏉ヤ骇鐢熸閿?/span>
_from.withdraw(amount)
lock_b.acquire() # 閿佷綇瀵规柟鐨勮处鎴?/span>
to.deposit(amount)
lock_b.release()
lock_a.release()
def transferb_a(_from, to, amount):
lock_a.acquire() # 閿佷綇鑷繁鐨勮处鎴?/span>
time.sleep(1) # 璁╀氦鏄撴椂闂村彉闀匡紝2涓氦鏄撶嚎绋嬫椂闂翠笂閲嶅彔锛屾湁瓒冲鏃堕棿鏉ヤ骇鐢熸閿?/span>
_from.withdraw(amount)
lock_b.acquire() # 閿佷綇瀵规柟鐨勮处鎴?/span>
to.deposit(amount)
lock_b.release()
lock_a.release()
lock_a = threading.Lock()
lock_b = threading.Lock()
a = Account(鈥?/span>a鈥?/span>, 1000)
b = Account(鈥?/span>b鈥?/span>, 1000)
#a寰€b杞处100
t1 = threading.Thread(target=transfera_b, args=(a, b, 100))
t1.start()
#b寰€a杞处200
t2 = threading.Thread(target=transferb_a, args=(b, a, 200))
t2.start()
t1.join()
t2.join()
print("a鐨勮处鎴蜂綑棰濓細",a.balance)
print("b鐨勮处鎴蜂綑棰濓細",b.balance)
# coding=utf-8
import threading
import hashlib
class Account:
def __init__(self, _id, balance):
self.id = _id
self.balance = balance
self.lock = threading.Lock()
def withdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
def transfer(_from, to, amount):
hasha,hashb = hashlock(_from, to)
if hasha >hashb:
_from.lock.acquire() # 閿佷綇鑷繁鐨勮处鎴?/span>
to.lock.acquire() # 閿佷綇瀵规柟鐨勮处鎴?/span>
#浜ゆ槗#################
_from.withdraw(amount)
to.deposit(amount)
#################
to.lock.release()
_from.lock.release()
elif hasha < hashb:
to.lock.acquire() # 閿佷綇鑷繁鐨勮处鎴?/span>
_from.lock.acquire() # 閿佷綇瀵规柟鐨勮处鎴?/span>
# 浜ゆ槗#################
_from.withdraw(amount)
to.deposit(amount)
#################
_from.lock.release()
to.lock.release()
else: ##hash鍊肩浉绛夛紝鏈€涓婂眰浣跨敤mylock閿侊紝浣犲彲浠ユ妸transfer鍋氭垚涓€涓被锛屾绫讳腑瀹炰緥涓€涓猰ylock銆?/span>
mylock.acquire()
_from.lock.acquire() # 閿佷綇鑷繁鐨勮处鎴?/span>
to.lock.acquire() # 閿佷綇瀵规柟鐨勮处鎴?/span>
# 浜ゆ槗#################
_from.withdraw(amount)
to.deposit(amount)
#################
to.lock.release()
_from.lock.release()
mylock.release()
def hashlock(_from,to):
hash1 = hashlib.md5()
hash1.update(bytes(_from.id, encoding=鈥?/span>utf-8鈥?/span>))
hasha = hash1.hexdigest()
hash = hashlib.md5()
hash.update(bytes(to.id, encoding=鈥?/span>utf-8鈥?/span>))
hashb = hash.hexdigest()
return hasha,hashb
a = Account(鈥?/span>a鈥?/span>, 1000)
b = Account(鈥?/span>b鈥?/span>, 1000)
mylock = threading.Lock()
#a寰€b杞处100
t1 = threading.Thread(target=transfer, args=(a, b, 100))
t1.start()
#b寰€a杞处200
t2 = threading.Thread(target=transfer, args=(b, a, 200))
t2.start()
t1.join()
t2.join()
print("a鐨勮处鎴蜂綑棰濓細",a.balance)
print("b鐨勮处鎴蜂綑棰濓細",b.balance)
import threading
from contextlib import contextmanager
# Thread-local state to stored information on locks already acquired
_local = threading.local()
@contextmanager
def acquire(*locks):
# Sort locks by object identifier
locks = sorted(locks, key=lambda x: id(x))
# Make sure lock order of previously acquired locks is not violated
acquired = getattr(_local,鈥?/span>acquired鈥?/span>,[])
if acquired and max(id(lock) for lock in acquired) >= id(locks[0]):
raise RuntimeError(鈥?/span>Lock Order Violation鈥?/span>)
# Acquire all of the locks
acquired.extend(locks)
_local.acquired = acquired
try:
for lock in locks:
lock.acquire()
yield
finally:
# Release locks in reverse order of acquisition
for lock in reversed(locks):
lock.release()
del acquired[-len(locks):]
import threading
x_lock = threading.Lock()
y_lock = threading.Lock()
def thread_1():
while True:
with acquire(x_lock, y_lock):
print(鈥?/span>Thread-1鈥?/span>)
def thread_2():
while True:
with acquire(y_lock, x_lock):
print(鈥?/span>Thread-2鈥?/span>)
t1 = threading.Thread(target=thread_1)
t1.daemon = True
t1.start()
t2 = threading.Thread(target=thread_2)
t2.daemon = True
t2.start()
6.姝婚攣妫€娴?/h3>