簡體   English   中英

有誰知道為什么我在使用多處理時遇到這個基本錯誤

[英]Does anyone know why i am getting this basic error when using multiprocessing

我使用多處理同時啟動 2 個功能,因為我需要它們並行工作,但我遇到了一個常見的基本錯誤,我不知道為什么。 我試圖這樣做調試python告訴我在使用它之前定義了服務器所以我有這個問題嗎?

def add_users():
    global users
    global client
    global client_addr
    users = []
    while True:
        print('2')
        server.listen(100)
        client, client_addr = server.accept()
        print(colored(f'[+] {client_addr} Client connected to the server', 'yellow'))
        users.append([client, client_addr])

def exec(c, cA):
    # others code

#@ray.remote
def mid():
    a = input(colored("#-> ", 'green'))

    if a == 'ls':
        if len(users) != 0:
            print(colored(users, 'yellow'))
            mid()
        else:
            print(colored("No user connected.", 'yellow'))
            mid()
    elif a.split(' ')[0] == 'connect':
        for t in len(users):
            if users[t][1] == a.split(' ')[1]:
                exec(users[t][0], users[t][1])

#ray.get([mid.remote(), add_users.remote()])

if __name__ == "__main__":
    colorama.init()
    #ray.init()
    os.system('color')

    print(colored(' ______     __  __        __    __     ______     __  __     ______     _____    ', 'red'))
    time.sleep(1)
    print(colored('/\  == \   /\ \_\ \      /\ "-./  \   /\  __ \   /\ \/\ \   /\  __ \   /\  __-.  ', 'red'))
    time.sleep(1)
    print(colored('\ \  __<   \ \____ \     \ \ \-./\ \  \ \ \/\ \  \ \ \_\ \  \ \  __ \  \ \ \/\ \ ', 'red'))
    time.sleep(1)
    print(colored(' \ \_____\  \/\_____\     \ \_\ \ \_\  \ \_____\  \ \_____\  \ \_\ \_\  \ \____- ', 'red'))
    time.sleep(1)
    print(colored('  \/_____/   \/_____/      \/_/  \/_/   \/_____/   \/_____/   \/_/\/_/   \/____/ \n', 'red'))

    HOST = '0.0.0.0'
    PORT = 8081
    global server
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((HOST, PORT))
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    print(server)
    print(colored('[+] Server Started', 'magenta'))
    print(colored('[+] Listening For Client Connection ...', 'magenta'))

    p1 = multiprocessing.Process(target=mid)
    p2 = multiprocessing.Process(target=add_users)

    p1.start()
    p2.start()

錯誤: server.listen(100) NameError: name 'server' is not defined

全局變量不在多個進程之間共享。 您需要將變量(即server )作為參數傳遞給進程p2

因此,您可以將add_users定義為具有server參數,如下所示:

def add_users(server):
    global users
    global client
    global client_addr
    users = []
    while True:
        print('2')
        server.listen(100)
        client, client_addr = server.accept()
        print(colored(f'[+] {client_addr} Client connected to the server', 'yellow'))
        users.append([client, client_addr])

並使用進程argsserver傳遞給add_user
p2 = multiprocessing.Process(target=add_users, args=(server,))

看看https://docs.python.org/3/library/multiprocessing.html#sharing-state-between-processes也許它會有所幫助。

暫無
暫無

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

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