새로운 concurrent.futures 클래스를 사용하여 간단한 소켓 서버를 만들려고합니다. 내가 ThreadPoolExecutor와 함께 잘 작동하도록 할 수는 있지만, ProcessPoolExecutor를 사용할 때 멈춰 버리고 그 이유를 이해할 수 없습니다. 상황을 감안할 때, 나는 그것이 피클 할 수없는 아이 프로세스에 무언가를 전달하려고 시도하는 것과 관련이 있다고 생각했지만, 그렇지 않습니다. 내 코드의 단순화 된 버전은 아래와 같습니다. 나는 어떤 조언을 주셔서 감사하겠습니다.Python 3 concurrent.futures 소켓 서버는 ThreadPoolExecutor와 함께 작동하지만 ProcessPoolExecutor에서는 작동하지 않습니다.
import concurrent.futures
import socket, os
HOST = ''
PORT = 9001
def request_handler(conn, addr):
pid = os.getpid()
print('PID', pid, 'handling connection from', addr)
while True:
data = conn.recv(1024)
if not data:
print('PID', pid, 'end connection')
break
print('PID', pid, 'received:', bytes.decode(data))
conn.send(data)
conn.close()
def main():
with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind((HOST, PORT))
sock.listen(10)
print("Server listening on port", PORT)
while True:
conn, addr = sock.accept()
executor.submit(request_handler, conn, addr)
conn.close()
print("Server shutting down")
if __name__ == '__main__':
main()
도움이된다면, 작업자가 심하게 죽으면 (예 : 종료 값 0이없는 경우) Python 3.2에서 ProcessPoolExecutor가 교착 상태가되는 것으로 알려져 있습니다. – donkopotamus
좋은 전화 @Donkopotamus, 당신은 코멘트 대신 대답으로 게시 했어야합니다. – ThatAintWorking