You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
987 B
Python
40 lines
987 B
Python
# обработка нескольких соединений одновременно, процессы и потоки
|
|
import socket
|
|
import threading
|
|
import multiprocessing
|
|
import os
|
|
|
|
def process_request(conn, addr):
|
|
print(f"connected client: {addr}")
|
|
with conn:
|
|
while True:
|
|
data = conn.recv(1024)
|
|
|
|
if not data:
|
|
break
|
|
|
|
print(data.decode("utf8"))
|
|
|
|
def worker(sock):
|
|
while True:
|
|
conn, addr = sock.accept()
|
|
print(f"pid {os.getpid()}")
|
|
th = threading.Thread(target=process_request, args=(conn, addr))
|
|
th.start()
|
|
|
|
with socket.socket() as sock:
|
|
sock.bind(("", 10001))
|
|
sock.listen()
|
|
|
|
workers_count = 5
|
|
workers_list = [
|
|
multiprocessing.Process(target=worker, args=(sock,))
|
|
for _ in range(workers_count)
|
|
]
|
|
|
|
for w in workers_list:
|
|
w.start()
|
|
|
|
for w in workers_list:
|
|
w.join()
|