因此,我try 构建以下设置:
- 拥有一个通过新线程处理传入连接的TCP服务器,
- 具有另一个进程,该进程创建连接到该TCP服务器的新进程(TCP客户端).
我在Windows 10笔记本电脑上使用的是Python3.11.2.
在大多数情况下,它都运行得很好.我只是偶尔会遇到与TCP客户端进程有关的问题.问题是有些进程不会被创建.这不能一直重现,有时我在创建5个客户端进程时会遇到问题,有时在创建150个时运行得很顺利.Tcp服务器socket.listen()Backlog已经足够高了.
服务器代码
import threading
import socket
MAX_DATASIZE = 2048;
HOST = "127.0.0.1"
PORT = 5000
def handleSingleSocket(conn, addr):
print("Client "+ str(addr[0]) + ": " + str(addr[1]) + " connected")
data = conn.recv(MAX_DATASIZE)
print(data)
conn.sendall(data)
conn.close()
print("Client "+ str(addr[0]) + ": " + str(addr[1]) + " disconnected")
listSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listSocket.bind((HOST, PORT))
listSocket.listen(200)
print(f"Server listening on address {HOST} and port {PORT}")
while True:
conn, addr = listSocket.accept()
thread = threading.Thread(target=handleSingleSocket,args=(conn,addr))
thread.start()
print(f"[ACTIVE CONNECTIONS]: {threading.active_count() - 1}" )
客户端代码(Hibernate 是让所有客户端同时连接)
import socket
import multiprocessing
import time
MAX_DATASIZE = 2048;
HOST = "127.0.0.1"
PORT = 5000
def handleSingleConnection(sock: socket.socket, i):
sock.connect((HOST, PORT))
time.sleep(2)
sock.sendall(f"Hello from Process {i}!".encode())
result = sock.recv(MAX_DATASIZE)
print(result)
sock.close()
if (__name__ == '__main__'):
for i in range(150):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
process = multiprocessing.Process(target=handleSingleConnection, args=(sock,i,))
process.start()
有些连接不会被创建.错误代码如下所示:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\erath\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\spawn.py", line 120, in spawn_main
exitcode = _main(fd, parent_sentinel)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\erath\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\spawn.py", line 130, in _main
self = reduction.pickle.load(from_parent)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\erath\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\reduction.py", line 238, in _rebuild_socket
return ds.detach()
^^^^^^^^^^^
File "C:\Users\erath\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\resource_sharer.py", line 38, in detach
with _resource_sharer.get_connection(self._id) as conn:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\erath\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\resource_sharer.py", line 86, in get_connection
c = Client(address, authkey=process.current_process().authkey)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\erath\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\connection.py", line 499, in Client c = PipeClient(address)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\erath\AppData\Local\Programs\Python\Python311\Lib\multiprocessing\connection.py", line 701, in PipeClient
_winapi.WaitNamedPipe(address, 1000)
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden
因此,我猜这与Windows上进程的衍生创建有关,但我真的无法理解它.非常感谢您的帮助,提前谢谢!:)