I create a new connection thread each time a connection with a client is made. However on the second time I run a client script, I get an error, how come?
The Client
from multiprocessing.connection import Client
conn = Client(('localhost', 5555), authkey='secret_password')
conn.send('Hello World!')
conn.close()
The Server
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)
The Error
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
You have a few problems with this server code.
You are closing the listener after the first connection
You arent looping for more connections
Disclaimer: I think is is kind of a messy way to write the server. I am only posting a fix for your current code 🙂
Here is a slightly cleaned up version of your same code. Still not 100% ideal but cleaner I think?