Windows에서 멀티 프로세싱 ServerApp
을 작동 시키려고합니다. 나는 os.fork()
기능이 없어서 socket
을 피할 수없는 방법으로 전달해야 할 것입니다 (?!).Python3 Windows multiprocessing이 소켓을 처리하도록 처리합니다.
내가 here 표시하지만, 그 방법은 파이썬 3에서 사용할 수없는 것처럼이 multiprocessing.reduction
에서 reduce_handle
및 rebuild_handle
를 사용 할 수있을 것으로 보았다 (?!). 사용할 수있는 duplicate
과 steal_handle
을 사용할 수는 있지만 사용할 방법이나 예제가 필요하지는 않습니다.
또한 새 프로세스를 만들 때 logging
이 문제가 될지 알고 싶습니다.
가 여기 내 ServerApp 샘플입니다 :
import logging
import socket
from select import select
from threading import Thread
from multiprocessing import Queue
from multiprocessing import Process
from sys import stdout
from time import sleep
class ServerApp(object):
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(stdout)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
def conn_handler(self, connection, address, buffer):
self.logger.info("[%d] - Connection from %s:%d", self.id, address[0], address[1])
try:
while True:
command = None
received_data = b''
readable, writable, exceptional = select([connection], [], [], 0) # Check for client commands
if readable:
# Get Command ... There is more code here
command = 'Something'
if command == 'Something':
connection.sendall(command_response)
else:
print(':(')
except Exception as e:
print(e)
finally:
connection.close()
self.client_buffers.remove(buffer)
self.logger.info("[%d] - Connection from %s:%d has been closed.", self.id, address[0], address[1])
def join(self):
while self.listener.is_alive():
self.listener.join(0.5)
def acceptor(self):
while True:
self.logger.info("[%d] - Waiting for connection on %s:%d", self.id, self.ip, self.port)
# Accept a connection on the bound socket and fork a child process to handle it.
conn, address = self.socket.accept()
# Create Queue which will represent buffer for specific client and add it o list of all client buffers
buffer = Queue()
self.client_buffers.append(buffer)
process = Process(target=self.conn_handler, args=(conn, address, buffer))
process.daemon = True
process.start()
self.clients.append(process)
# Close the connection fd in the parent, since the child process has its own reference.
conn.close()
def __init__(self, id, port=4545, ip='127.0.0.1', method='tcp', buffer_size=2048):
self.id = id
self.port = port
self.ip = ip
self.socket = None
self.listener = None
self.buffer_size = buffer_size
# Additional attributes here....
self.clients = []
self.client_buffers = []
def run(self):
# Create TCP socket, bind port and listen for incoming connections
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((self.ip, self.port))
self.socket.listen(5)
self.listener = Thread(target=self.acceptor) # Run acceptor thread to handle new connection
self.listener.daemon = True
self.listener.start()
일부 코드를 작성했지만 어떤 '프로토콜'정의도 볼 수 없습니다. 이미 수락 된 경우 수용 규칙을 정의 할 수 없습니다 (필터 란 무엇입니까?). – dsgdfg
@dsgdfg 내가 맞았는지 확실하지 않지만 각 연결을 수락하고 별도의 프로세스에서 처리해야합니다. – sstevan