繁体   English   中英

如何将列表的元素作为python中线程的参数传递

[英]How to pass elements of a list as arguments for a thread in python

我目前正在编写一个接受多个客户端的服务器,为每个客户端创建一个线程并通过相同的函数handle_client传递它们。 我正在创建一个连接到服务器的每个客户端的列表并将其存储在clients中。 我想通过不同的功能块传递每个客户端,但我不确定如何将列表的元素作为线程的参数传递。 因此,例如,在我的情况下,两个客户端是RobotIconet ,我想为每个客户端创建一个不同的handle_client函数,然后我希望通过为它设计的handle_client函数和客户端Iconet传递客户端Robot通过为它设计的不同的handle_client 这是服务器端的代码。

import threading 
import socket 

PORT = 1026
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER,PORT)
FORMAT = "utf-8"
HEADER = 1024
DISCONNECT_MESSAGE = "END_CYCLE"
VITA_R = 'Robot: yes'
VITA_I = 'Iconet: yes'
vita_robot = False
vita_iconet = False


server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
clients = []
aliases = []
alias_dictionary_iter = zip(aliases,clients)
alias_dictionary = dict(alias_dictionary_iter)

def broadcast(values_iconet):
    for client in clients:
        client.send(values_iconet)
        

def handle_client(client,addr):
    print(f"[NEW CONNECTION] {addr} connected.")
    connected = True
    while connected:
        for client in clients:
         if client == clients[0]:
            robot_message = 'VITA_R'
            client.send(robot_message.encode(FORMAT))
            robot_response = client.recv(2048).decode(FORMAT)
            if robot_response == VITA_R:
                print(robot_response)
                print (type(robot_response))
                print("VITA received from robot")
                vita_robot == True
                    
            else:
                print("VITA not received from robot")
                vita_robot == False
                   
         else:
            iconet_message = 'VITA_I'
            client.send(iconet_message.encode(FORMAT))
            iconet_response = client.recv(2048).decode(FORMAT)
            if iconet_response == VITA_I:
                print(iconet_response)
                print (type(iconet_response))
                print("VITA received from iconet")
                vita_iconet == True
                    
            else:
                print("VITA not received from iconet")
                vita_iconet == False
                continue

            client.send('LOCA'.encode(FORMAT))
            values_iconet = client.recv(HEADER)
            print(values_iconet)
            broadcast(values_iconet)
            continue


def start():
    server.listen()
    print(f"[LISTENING] Server is listening on {SERVER}")
    while True:
        client, addr = server.accept()
        print(f"[NEW CONNECTION] {addr} connected.")
        client.send('NAME?'.encode(FORMAT))
        alias = client.recv(1024)
        aliases.append(alias)
        clients.append(client)
        print(f'The clients is {alias}'.encode(FORMAT))
        thread = threading.Thread(target= handle_client, args=(client, addr))
        thread.start()



print ('[STARTING] server is starting')
start()

为此创建一个字典

functions_dict = {“机器人”:handle_robot,“Iconet”:handle_client}

thread = threading.Thread(target= functions_dict[client] , args=(client, addr))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM