1

Topic: Sockets: it is impossible to process entering connections

server.py

#!/usr/bin/env python
import sys
from multiprocessing import Process, Queue
from multiprocessing.connection import Listener
from queue import Empty
from time import sleep
class Worker (Process):
def __ init __ (self, q):
super (). __ init __ (daemon=True)
self.q = q
print (' start process')
sys.stdout.flush ()
self.start ()
def run (self):
while 1:
try:
client = self.q.get ()
try:
data = client.recv_bytes ()
except EOFError:
# Connection is broken off by the client
print (' connection is lost ')
sys.stdout.flush ()
continue
print (' received: {} '.format (data.decode())
sys.stdout.flush ()
client.send_bytes (b'OK ')
# We add client connection in the queue end
self.q.put (client)
except Empty:
print (' queue is empty ')
sys.stdout.flush ()
# sleep is added to restrict only speed of an output in the console
sleep (1)
if __ name __ == ' __ main __ ':
listener = Listener ((' localhost ', 9000), authkey=b'secret ')
q = Queue ()
# We will have 10 processes for operation with client connections
print (' start worker processes')
for i in range (10):
Worker (q)
print (' start server ')
while 1:
client = listener.accept ()
print (' client: ', client)
q.put (client)
$ ./server.py
start worker processes
start process
start process
start process
start process
start process
start process
start process
start process
start process
start process
start server
client: <multiprocessing.connection. Connection object at 0x7f9a41bcce80>
client: <multiprocessing.connection. Connection object at 0x7f9a41bd17f0>
client: <multiprocessing.connection. Connection object at 0x7f9a41bd1710>
client: <multiprocessing.connection. Connection object at 0x7f9a41bd1748>
client: <multiprocessing.connection. Connection object at 0x7f9a41bcce80>
connection is lost
connection is lost
connection is lost
connection is lost
connection is lost
client: <multiprocessing.connection. Connection object at 0x7f9a41bdf198>
client: <multiprocessing.connection. Connection object at 0x7f9a41bcce80>
client: <multiprocessing.connection. Connection object at 0x7f9a41bdf198>
connection is lost
connection is lost
connection is lost
client: <multiprocessing.connection. Connection object at 0x7f9a41bd1748>
connection is lost

It generally hangs also deduces nothing
test.py

#!/usr/bin/env python
import sys
from multiprocessing import Pool
from multiprocessing.connection import Client
def test_client (i):
client = Client ((' localhost ', 9000), authkey=b'secret ')
client.send_bytes (b'hello from client # {} '.format (i))
data = client.recv_bytes ()
print (' client # {} received: ', data.decode ())
sys.stdout.flush ()
if __ name __ == ' __ main __ ':
p = Pool ()
for i in range (10):
p.apply_async (test_client, (i,))
p.close ()
p.join ()

2

Re: Sockets: it is impossible to process entering connections

The patriot of Israel;

#!/usr/bin/env python
import traceback
from multiprocessing import Process
from multiprocessing.connection import Listener
class Worker (Process):
def __ init __ (self, conn):
super (). __ init __ ()
self.daemon=True
self.conn = conn
def run (self):
while 1:
try:
data = self.conn.recv ()
except Exception:
traceback.print_exc ()
break
print (' received: ', data)
self.conn.send (' OK ')
if __ name __ == ' __ main __ ':
listener = Listener ((' localhost ', 9000), authkey=b'secret ')
while 1:
try:
conn = listener.accept ()
Worker (conn).start ()
except Exception:
traceback.print_exc ()
#!/usr/bin/env python
from multiprocessing import Pool
from multiprocessing.connection import Client
def test_client (i):
conn = Client ((' localhost ', 9000), authkey=b'secret ')
conn.send (' hello from client # {} '.format (i))
data = conn.recv ()
return (i, data)
if __ name __ == ' __ main __ ':
# for i in range (10):
# conn = Client ((' localhost ', 9000), authkey=b'secret ')
# conn.send (' hello from client # {} '.format (i))
# data = conn.recv ()
# print (i, data)
p = Pool ()
results = p.map (test_client, range (10))
print (results)
p.close ()
p.join ()

Server output:

$ ./server.py
received: hello from client #1
received: hello from client #2
received: hello from client #0
connection closed
connection closed
connection closed
received: hello from client #8
connection closed
received: hello from client #9
connection closed
received: hello from client #4
received: hello from client #3
received: hello from client #6
connection closed
connection closed
connection closed
received: hello from client #7
connection closed
^CTraceback (most recent call last):
File "./server.py", line 26, in <module>
conn = listener.accept ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 453, in accept
c = self. _ listener.accept ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 593, in accept
s, self. _ last_accepted = self. _ socket.accept ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/socket.py", line 205, in accept
fd, addr = self. _ accept ()
KeyboardInterrupt
(py3) sergey@sergey-Lenovo-Y520-15IKBN:~/code/threads_and_processes/multiprocessing_server$./server.py
received: hello from client #1
received: hello from client #0
connection closed
connection closed
received: hello from client #2
connection closed
received: hello from client #8
received: hello from client #9
connection closed
connection closed
received: hello from client #6
connection closed
received: hello from client #5
connection closed
^CTraceback (most recent call last):
File "./server.py", line 26, in <module>
try:
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 453, in accept
c = self. _ listener.accept ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 593, in accept
s, self. _ last_accepted = self. _ socket.accept ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/socket.py", line 205, in accept
fd, addr = self. _ accept ()
KeyboardInterrupt
(py3) sergey@sergey-Lenovo-Y520-15IKBN:~/code/threads_and_processes/multiprocessing_server$./server.py
received: hello from client #0
received: hello from client #2
received: hello from client #1
Traceback (most recent call last):
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
Traceback (most recent call last):
EOFError
EOFError
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
received: hello from client #9
received: hello from client #8
Traceback (most recent call last):
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
EOFError
received: hello from client #3
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
received: hello from client #4
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
received: hello from client #6
received: hello from client #7
Traceback (most recent call last):
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
EOFError
received: hello from client #5
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
received: hello from client #2
received: hello from client #0
received: hello from client #1
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
Traceback (most recent call last):
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
EOFError
received: hello from client #8
received: hello from client #9
Traceback (most recent call last):
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
EOFError
received: hello from client #4
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
received: hello from client #5
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
received: hello from client #6
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError
received: hello from client #7
Traceback (most recent call last):
File "./server.py", line 16, in run
data = self.conn.recv ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 250, in recv
buf = self. _ recv_bytes ()
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 407, in _recv_bytes
buf = self. _ recv (4)
File "/home/sergey/.pyenv/versions/3.6.5/lib/python3.6/multiprocessing/connection.py", line 383, in _recv
raise EOFError
EOFError

Only time worked normally:

$ ./test.py
[(0, ' OK '), (1, ' OK '), (2, ' OK '), (3, ' OK '), (4, ' OK '), (5, ' OK '), (6, ' OK '), (7, ' OK '), (8, ' OK '), (9, ' OK ')]

When requests came as sequence.

3

Re: Sockets: it is impossible to process entering connections

The patriot of Israel, I understood. A problem in library multiprocessing on-course. I with the help  tried  to make set of requests:

#!/usr/bin/env bash
# The AND informs the shell to put the command in the background
for i in {0. 10}
do
./client.py - payload "{\" method \": \" echo \", \" params \": [\" Echo! \"], \" id \": $i}">> out.txt AND
done
{"jsonrpc": "2.0", "result":"Echo!","id": 0}
{"jsonrpc": "2.0", "result":"Echo!","id": 1}
{"jsonrpc": "2.0", "result":"Echo!","id": 3}
{"jsonrpc": "2.0", "result":"Echo!","id": 4}
{"jsonrpc": "2.0", "result":"Echo!","id": 6}
{"jsonrpc": "2.0", "result":"Echo!","id": 9}
{"jsonrpc": "2.0", "result":"Echo!","id": 8}
{"jsonrpc": "2.0", "result":"Echo!","id": 7}
{"jsonrpc": "2.0", "result":"Echo!","id": 2}
{"jsonrpc": "2.0", "result":"Echo!","id": 10}
{"jsonrpc": "2.0", "result":"Echo!","id": 5}

All works