Python Forum
Multiprocesing socket - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Multiprocesing socket (/thread-34810.html)



Multiprocesing socket - samuelbachorik - Sep-02-2021

Hi iam trying to sent numpy array to multiprocesing socket and always getting error
Does somebody know where is problem ?
This is my server
from multiprocessing.connection import Listener

address = ('localhost', 6000) 
listener = Listener(address, authkey= 'secret password'.encode('utf-8'))
conn = listener.accept()

while True:
    msg = conn.recv()
    print(msg)
And this is my client that sent numpy array to server

from multiprocessing.connection import Client
import numpy as np

a = np.zeros(5)
address = ('localhost', 6000)
conn = Client(address, authkey= 'secret password'.encode('utf-8'))
conn.send(a)

conn.close()
And iam getting this output-

server actually prints out that numpy array but anyway it throws error.
Error:
Traceback (most recent call last): File "C:/Users/samue/PycharmProjects/CONDA/test1.py", line 14, in <module> msg = conn.recv() File "C:\Users\samue\anaconda3\lib\multiprocessing\connection.py", line 250, in recv buf = self._recv_bytes() File "C:\Users\samue\anaconda3\lib\multiprocessing\connection.py", line 414, in _recv_bytes buf = self._recv(4) File "C:\Users\samue\anaconda3\lib\multiprocessing\connection.py", line 383, in _recv raise EOFError [0. 0. 0. 0. 0.] EOFError



RE: Multiprocesing socket - bowlofred - Sep-02-2021

Quote:recv()

Return an object sent from the other end of the connection using send(). Blocks until there is something to receive. Raises EOFError if there is nothing left to receive and the other end was closed.

So the EOFError is expected when the data is done. If you don't want this to exit your program, you need to put it in a try/except and handle the case where the exception is thrown.

while True:
    try:
        msg = conn.recv()
        print(msg)
    except EOFError:
        print("Connection closed.")
        break()



RE: Multiprocesing socket - samuelbachorik - Sep-02-2021

(Sep-02-2021, 05:46 PM)bowlofred Wrote:
Quote:recv()

Return an object sent from the other end of the connection using send(). Blocks until there is something to receive. Raises EOFError if there is nothing left to receive and the other end was closed.

So the EOFError is expected when the data is done. If you don't want this to exit your program, you need to put it in a try/except and handle the case where the exception is thrown.

while True:
    try:
        msg = conn.recv()
        print(msg)
    except EOFError:
        print("Connection closed.")
        break()

Thank you a lot now i uderstand !