创建新线程时将python函数对象传递到threading.Thread构造函数中-为什么?

创建新线程时将python函数对象传递到threading.Thread构造函数中-为什么?,python,multithreading,tcp,client-server,Python,Multithreading,Tcp,Client Server,我正在编写一个基于客户机-服务器模型的TCP消息传递应用程序,该模型由一个服务器和多个并发通信的客户机组成。我对多线程知之甚少,所以为了更好地理解如何做到这一点,我在网上找到了一些代码 运营代码:1/2 def recv_handler(): global t_lock global clients global serverSocket print('Server is up.') while True: # create a new c

我正在编写一个基于客户机-服务器模型的TCP消息传递应用程序,该模型由一个服务器和多个并发通信的客户机组成。我对多线程知之甚少,所以为了更好地理解如何做到这一点,我在网上找到了一些代码

运营代码:1/2

def recv_handler():
    global t_lock
    global clients
    global serverSocket
    print('Server is up.')
    while True:
        # create a new connection for a new client
        connection_socket, client_address = serverSocket.accept()

        # create a new function handler for the client
        socket_handler = connection_handler(connection_socket, client_address)

        # create a new thread for the client socket
        socket_thread = threading.Thread(name=str(client_address), target=socket_handler)
        socket_thread.daemon = False
        socket_thread.start()
# return a function as connection handler for a specific socket for multi threading
def connection_handler(connection_socket, client_address):
    def real_connection_handler():
        while True:
            data = connection_socket.recv(1024)
            ....
            more logic

    return real_connection_handler
def recv_handler():
    global t_lock
    global clients
    global serverSocket
    print('Server is up.')
    while True:
        # create a new connection for a new client
        connection_socket, client_address = serverSocket.accept()

        # create a new thread for the client socket
        socket_thread = threading.Thread(name=str(client_address),target=connection_handler(connection_socket, client_address))
        socket_thread.daemon = False
        socket_thread.start()
def connection_handler(connection_socket, client_address):
        while True:
            data = connection_socket.recv(1024)
            ....
            more logic
运营代码:2/2

def recv_handler():
    global t_lock
    global clients
    global serverSocket
    print('Server is up.')
    while True:
        # create a new connection for a new client
        connection_socket, client_address = serverSocket.accept()

        # create a new function handler for the client
        socket_handler = connection_handler(connection_socket, client_address)

        # create a new thread for the client socket
        socket_thread = threading.Thread(name=str(client_address), target=socket_handler)
        socket_thread.daemon = False
        socket_thread.start()
# return a function as connection handler for a specific socket for multi threading
def connection_handler(connection_socket, client_address):
    def real_connection_handler():
        while True:
            data = connection_socket.recv(1024)
            ....
            more logic

    return real_connection_handler
def recv_handler():
    global t_lock
    global clients
    global serverSocket
    print('Server is up.')
    while True:
        # create a new connection for a new client
        connection_socket, client_address = serverSocket.accept()

        # create a new thread for the client socket
        socket_thread = threading.Thread(name=str(client_address),target=connection_handler(connection_socket, client_address))
        socket_thread.daemon = False
        socket_thread.start()
def connection_handler(connection_socket, client_address):
        while True:
            data = connection_socket.recv(1024)
            ....
            more logic
表面上看,我想我理解这里发生的事情:

recv\u handler
不断侦听新的客户端连接,并根据请求为每个新的客户端连接创建一个新的专用套接字。这是通过
connection\u socket、client\u address=serverSocket.accept()实现的
创建新套接字后,套接字和地址将传递给
connection\u handler(connection\u socket,client\u address)
函数,返回对象
real\u connection\u handler()
。 然后创建一个新线程,并将real_connection_handler对象作为线程构造函数中的目标函数


OP为什么这样做?具体来说,为什么OP将target设置为内部函数(real_connection_handler)而不是外部函数(connection_handler)?他为什么在这里嵌套函数?为什么不干脆做:

我的代码建议1/2

def recv_handler():
    global t_lock
    global clients
    global serverSocket
    print('Server is up.')
    while True:
        # create a new connection for a new client
        connection_socket, client_address = serverSocket.accept()

        # create a new function handler for the client
        socket_handler = connection_handler(connection_socket, client_address)

        # create a new thread for the client socket
        socket_thread = threading.Thread(name=str(client_address), target=socket_handler)
        socket_thread.daemon = False
        socket_thread.start()
# return a function as connection handler for a specific socket for multi threading
def connection_handler(connection_socket, client_address):
    def real_connection_handler():
        while True:
            data = connection_socket.recv(1024)
            ....
            more logic

    return real_connection_handler
def recv_handler():
    global t_lock
    global clients
    global serverSocket
    print('Server is up.')
    while True:
        # create a new connection for a new client
        connection_socket, client_address = serverSocket.accept()

        # create a new thread for the client socket
        socket_thread = threading.Thread(name=str(client_address),target=connection_handler(connection_socket, client_address))
        socket_thread.daemon = False
        socket_thread.start()
def connection_handler(connection_socket, client_address):
        while True:
            data = connection_socket.recv(1024)
            ....
            more logic
我的代码建议2/2

def recv_handler():
    global t_lock
    global clients
    global serverSocket
    print('Server is up.')
    while True:
        # create a new connection for a new client
        connection_socket, client_address = serverSocket.accept()

        # create a new function handler for the client
        socket_handler = connection_handler(connection_socket, client_address)

        # create a new thread for the client socket
        socket_thread = threading.Thread(name=str(client_address), target=socket_handler)
        socket_thread.daemon = False
        socket_thread.start()
# return a function as connection handler for a specific socket for multi threading
def connection_handler(connection_socket, client_address):
    def real_connection_handler():
        while True:
            data = connection_socket.recv(1024)
            ....
            more logic

    return real_connection_handler
def recv_handler():
    global t_lock
    global clients
    global serverSocket
    print('Server is up.')
    while True:
        # create a new connection for a new client
        connection_socket, client_address = serverSocket.accept()

        # create a new thread for the client socket
        socket_thread = threading.Thread(name=str(client_address),target=connection_handler(connection_socket, client_address))
        socket_thread.daemon = False
        socket_thread.start()
def connection_handler(connection_socket, client_address):
        while True:
            data = connection_socket.recv(1024)
            ....
            more logic
我的建议代码是否会引发资源竞争?就像在访问连接处理器函数时形成队列一样?例如,如果要与服务器建立第一个客户机连接请求(称为conn_1)并在连接处理程序函数中保持活动状态,而要建立第二个客户机连接请求(称为conn_2),则conn_2是否只有在conn_1退出后才能访问连接处理程序函数(即停止发送消息)


这是我有史以来的第一篇帖子,所以我希望它是清楚的。我被他所做的事情弄糊涂了,python对我来说也是新的,所以我的问题对我来说也是混乱的。我很欣赏任何清晰的内容。提前感谢。

“为什么OP将目标设置为函数对象而不是函数本身?”我真的不明白你的意思…函数对象就是函数本身是的,你是对的,措辞很糟糕。但问题仍然存在,为什么OP nest real_connection_handler()在connection_handler()中?为什么不按照我的建议做呢?