Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复多处理echo服务器以处理多个客户端_Python_Multithreading_Sockets_Python Multiprocessing - Fatal编程技术网

Python 如何修复多处理echo服务器以处理多个客户端

Python 如何修复多处理echo服务器以处理多个客户端,python,multithreading,sockets,python-multiprocessing,Python,Multithreading,Sockets,Python Multiprocessing,我想创建一个多处理echo服务器。我目前正在使用telnet作为我的客户端向echo服务器发送消息。目前我可以处理一个telnet请求,它会回显响应。起初,我认为无论何时创建套接字,都应该初始化pid。对吗 如何允许多个客户端使用多处理连接到我的服务器 #!/usr/bin/env python import socket import os from multiprocessing import Process def create_socket(): # Create socke

我想创建一个多处理echo服务器。我目前正在使用telnet作为我的客户端向echo服务器发送消息。目前我可以处理一个telnet请求,它会回显响应。起初,我认为无论何时创建套接字,都应该初始化pid。对吗

如何允许多个客户端使用多处理连接到我的服务器

#!/usr/bin/env python
import socket
import os
from multiprocessing import Process

def create_socket():

    # Create socket
    sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Port for socket and Host
    PORT = 8002
    HOST = 'localhost'

    # bind the socket to host and port
    sockfd.bind((HOST, PORT))
    # become a server socket
    sockfd.listen(5)

    start_socket(sockfd)

def start_socket(sockfd):

    while True:

        # Establish and accept connections woth client
        (clientsocket, address) = sockfd.accept()

         # Get the process id.
        process_id = os.getpid()
        print("Process id:", process_id)

        print("Got connection from", address)
        # Recieve message from the client
        message = clientsocket.recv(2024)
        print("Server received: " + message.decode('utf-8'))
        reply = ("Server output: " + message.decode('utf-8'))
        if not message:
            print("Client has been disconnected.....")
            break
        # Display messags.
        clientsocket.sendall(str.encode(reply))

    # Close the connection with the client
    clientsocket.close()

if __name__ == '__main__':

    process = Process(target = create_socket)     
    process.start()

设置服务器、绑定、侦听等的初始部分(您的
create_socket
)应该在主进程中


一旦你接受并获得一个套接字,你应该产生一个单独的进程来处理这个连接。换句话说,您的
start\u套接字应该在一个单独的进程中生成,并且应该永远循环

了解哪些阻塞系统调用,哪些不阻塞系统调用可能是个好主意<例如,code>listen
不是阻塞,而
accept
是阻塞。因此,基本上-您通过
进程(..)
创建了一个进程,该进程在
接受时阻塞,并且在建立连接时-处理该连接

您的代码应该具有如下结构(伪代码)


我还必须提到,虽然这是一个很好的方法来理解事情是如何完成的,但为每个连接启动一个新的过程并不是一个好主意

在handle_connection()中,我将回显消息?因此,我不需要main,在哪里调用client\u process.start()是的,在
handle\u connection
中,您应该回显消息。您可以执行
client\u进程。立即启动
那么,我将把os.getpid()放在哪里?为什么要把
os.getpid()放在那里
os.getpid()
-将返回当前进程的进程ID。因此,无论您想要哪个进程的
PID
,都应该将
os.getpid()
放在那里。因此,如果您想了解客户端进程的
pid
,请将其放入
handle\u connection
@pennyBoy中,不要放弃。当你进入“出于某种原因”阶段时,乐趣就开始了。你可以在地平线上看到它。:)它起了点作用。但现在,每当我创建一个新流程时,它似乎都会阻塞。我会更新我的代码。

def handle_connection(accepted_socket):
    # do whatever you want with the socket
    pass

def server():

    # Create socket and listen to it. 
    sock = socket.socket(....)
    sock.bind((HOST, PORT))
    sock.listen(5)

    while True:
        new_client = sock.accept() # blocks here.

        # unblocked 
        client_process = Process(target=handle_connection, args=(new_client))
        client_process.start()