簡體   English   中英

全局鎖導致我的程序停止運行

[英]global lock causing my program to stop running

每當我運行其中包含threading.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)

在全球鎖工作正常deleteUser_InHash()函數,但在停止程序addUser_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_InHash()”里面

大家注意:我變了

global_lock = Lock()

global_lock = RLock()

現在我的程序運行良好,我相信這與 RLock 允許線程多次重新獲取鎖有關,而普通鎖似乎不能?

來源:https ://docs.python.org/3/library/threading.html#thread-objects

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM