全局锁,即使没有其他线程,也无法执行其余代码

时间:2018-10-07 21:19:39

标签: python multithreading locking

def addUser_InHash(username, password, clearanceLvl):
    print("Adding user in hash")
    hashID = 0
    hashString = username + password
    hashIncIndex = 0  # This is used to increment "hashIncrement"
    added = False
    for i in hashString:
        hashID += ord(i)
    hashID = hashID % hashKey
    print(hashID, "hashID in addUser")
    try:
        with open("LoginHashTable.pickle", "rb") as file:
            saved_data = pickle.load(file)
            print("Original")
            print(saved_data)

    except:
        saved_data = {}
    print("File opened")


    while not added:
    # Make this so that it can check for unique usernames, using AND statements, and two variables
        if hashID in saved_data:
            print()
            if hashID == (hashKey - 1):
                hashID = 0
            else:
                hashID += hashIncrement[hashIncIndex]

            if hashIncIndex != 3:
                hashIncIndex += 1  # Increments hashIncrement
            else:
                hashIncIndex = 0
        else:
            print("User doesnt exist, adding to hash table")
            saved_data[hashID] = [username, password]
            print("New Added")
            print(saved_data)
            added = True
    print("Saving updated file addUser_InHash")
    with global_lock:
        print("past global lock")
        with open("LoginHashTable.pickle", "wb") as file:
            pickle.dump(saved_data, file)
            print(saved_data)
            print("Data saved")

由于某些奇怪的原因,此python代码在“ with global_lock”部分停止。 全局锁定义为

global_lock = Lock()

我的线程定义为

connThread = Thread(target=handler, args=(conn, addr))

这是由另一个函数调用的,需要完成,以便我的客户端(联系此代码所在的服务器)可以继续 每次重新启动服务器时,此时都没有其他线程在运行。

0 个答案:

没有答案