Python 全局锁导致我的程序停止运行
每当我运行程序中包含Python 全局锁导致我的程序停止运行,python,python-3.x,locking,python-multithreading,Python,Python 3.x,Locking,Python Multithreading,每当我运行程序中包含线程.Lock的部分时,我的程序就会完全停止运行(它不会崩溃,只是暂停) 我需要它,因为它是一个服务器,多个客户端可能正在连接并试图同时覆盖所有数据。在运行此程序时,只有一个线程处于活动状态,客户端连接到该线程。我也将其用于我的Sqlite3数据库。我没有注意到它在那里造成了问题,因为尽管有全局锁,它似乎运行得很好。所有这些都是以相同的格式完成的 with global_lock: 这里是线程的起始位置,以及我如何导入线程 from threading import Thr
线程.Lock
的部分时,我的程序就会完全停止运行(它不会崩溃,只是暂停)
我需要它,因为它是一个服务器,多个客户端可能正在连接并试图同时覆盖所有数据。在运行此程序时,只有一个线程处于活动状态,客户端连接到该线程。我也将其用于我的Sqlite3数据库。我没有注意到它在那里造成了问题,因为尽管有全局锁,它似乎运行得很好。所有这些都是以相同的格式完成的
with global_lock:
这里是线程的起始位置,以及我如何导入线程
from threading import Thread, Lock
global_lock = Lock()
while True:
conn, addr = s.accept()
connThread = Thread(target=handler, args=(conn, addr))
connThread.daemon = True
connThread.start()
这是节目单
def addUser_InHash(username, password):
print("Adding user in hash")
hashID = 0
hashString = username + password
added = False
for i in hashString:
hashID += ord(i)
hashID = hashID % hashKey
print(hashID, "hashID in addUser")
file = open("LoginHashTable.pickle", "rb+")
if os.path.getsize("LoginHashTable.pickle") > 0:
hashTable = pickle.load(file)
print("File not empty,\nSaved Data:\n{}".format(hashTable))
else:
print("File empty")
hashTable = {}
count = 0
while not added:
print("while not count :", count)
count += 1
if hashID in hashTable:
# If this index exists
if hashID > (hashKey - 1):
hashID = 0
else:
hashID += 1
if hashID > (hashKey - 1):
hashID = 0
else:
print("User doesnt exist, adding to hash table")
hashTable[hashID] = [username, password]
print("New Added")
print(hashTable)
added = True
print("Saving updated file addUser_InHash")
if hashTable:
with global_lock:
file.seek(0) # Move file pointer back to beginning of file
file.truncate() # Empty file by truncating to current file pointer position
pickle.dump(hashTable, file)
print(hashTable)
print("Data saved")
file.close()
else:
print("Hash table still empty, addUser_InHash")
def deleteUser_InHash(username, password):
print("In deleteUser_InHash\nUsername: {}\nPassword: {}".format(username,password))
dataFound = True
hashID = 0
count = 0
hashString = username + password
if os.path.getsize("LoginHashTable.pickle") > 0:
file = open("LoginHashTable.pickle", "rb+")
hashTable = pickle.load(file)
print("File not empty,\nSaved Data:\n{}".format(hashTable))
else:
print("File empty")
dataFound = False
if dataFound:
print("datafound true")
for i in hashString:
hashID += ord(i)
hashID = hashID % hashKey
print("hashID:",hashID)
try:
print("In try")
while dataFound:
print("In while, count:",count)
if count == hashKey:
dataFound = False
if hashTable[hashID] == [username,password]:
del hashTable[hashID]
print("Outside global lock")
with global_lock:
print("Inside global lock")
file.seek(0) # Move file pointer back to beginning of file
file.truncate() # Empty file by truncating to current file pointer position
pickle.dump(hashTable, file)
print(hashTable)
print("Data saved")
file.close()
print("Outside global lock")
print("Data updated")
print("User :", username, "deleted")
break
else:
hashID += 1
count += 1
except IndexError:
print("username could not be found")
return False
else:
return False
这两个函数按以下顺序调用:
deleteUser_InHash(username1,password1)
addUser_InHash(username2,password2)
with global lock在deleteUser\u InHash()
函数中工作正常,但在addUser\u InHash()
中停止程序
程序挂在这里:
{33: ['foo', 'bar'], 0: ['toni', 'tony'], 34: ['bar', 'foo'], 118: ['fo', 'la'], 8: ['Tom', 'Tom'], 262: ['Kam', 'Kam'], 258: ['yes', 'no']}
Saving updated file addUser_InHash
将代码挂在线路上:
if hashTable:
with global_lock:
我知道这是真的,因为它永远不会出现在打印语句中:
print(hashTable)
print("Data saved")
在“addUser\u InHash()”内所有人注意:我改变了
global_lock = Lock()
到
现在我的程序运行良好,我相信这与RLock允许线程多次重新获取锁有关,而普通锁似乎不能
资料来源:你知道它挂在哪里吗?我已经编辑了它,这很有帮助,但我的意思是确切在哪一行。您还需要添加启动两个线程运行的代码。有可能它们都处于死锁状态(都在等待
锁
被释放,但我还不能确定。在我测试此代码时,只有一个线程正在运行。我会尽全力,谢谢您的支持
global_lock = RLock()