クライアントとの接続が確立されるたびに新しい接続スレッドを作成します。しかし、2回目にクライアントスクリプトを実行すると、エラーが発生します。どのように来るのですか?再帰的接続スレッドの作成
クライアント
from multiprocessing.connection import Client
conn = Client(('localhost', 5555), authkey='secret_password')
conn.send('Hello World!')
conn.close()
import time
from multiprocessing.connection import Listener
from threading import Thread
_threads = []
_listener = Listener(('localhost', 5555), authkey='secret_password')
def start_server_thread():
global _threads
_threads.append(Thread(target=threaded_server))
_threads[-1].daemon = True
_threads[-1].start()
def threaded_server():
conn = _listener.accept()
print str(conn.recv())
conn.close()
_listener.close()
if __name__ == "__main__":
start_server_thread()
while True:
time.sleep(1)
エラー
Traceback (most recent call last):
File "C:\dev\spyker\t2.py", line 3, in <module>
conn = Client(('localhost', 5555), authkey='secret_password')
File "C:\Python26\lib\multiprocessing\connection.py", line 143, in Client
c = SocketClient(address)
File "C:\Python26\lib\multiprocessing\connection.py", line 263, in SocketClient
s.connect(address)
File "<string>", line 1, in connect
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
あなたは正しいです。私の主な問題は、リスナーを閉じて、新しい接続を受け入れることをループしなかったことです。 – Jonathan
@Jonathan - スレッドループハンドラに接続を渡す方法を示す2番目の例を更新したので、サーバーループdoesntブロック – jdi