Reputation: 141
I'm developing a simple application to test Pipes in Python. The application starts a new process, that works as "server". The "server" starts another two worker processes, that send some data to the "server" When "server" receives a message, it is supposed to update its state, send it to all workers and also to the main process. But the application always stops after receiving 278 messages. What am I doing wrong? Here is the code:
class Server ( multiprocessing.Process ):
def __init__( self, connection ):
super( Server, self ).__init__()
self.conn = connection
def run( self ):
state = []
conn1a, conn1b = multiprocessing.Pipe()
conn2a, conn2b = multiprocessing.Pipe()
p1 = multiprocessing.Process( target=worker, args=(conn1b, 2, 'p1', 1 ) )
p2 = multiprocessing.Process( target=worker, args=(conn2b, 3, 'p2', -1 ) )
p1.start()
p2.start()
counter = 0
while 1:
if conn1a.poll() or conn2a.poll():
if conn1a.poll():
state = conn1a.recv()
if conn2a.poll():
state = conn2a.recv()
self.conn.send( state )
conn1a.send( state )
conn2a.send( state )
counter += 1
print "counter", counter
p1.join()
p2.join()
def worker( conn, seconds, name, multiplier ):
x = 0
conn.send( [name, x*multiplier] )
while 1:
if conn.poll():
x += seconds
time.sleep( seconds )
conn.send( [name, x*multiplier] )
if __name__ == '__main__':
conn1, conn2 = multiprocessing.Pipe( False )
p = Server( conn2 )
p.start()
while 1:
print "received", conn1.recv()
p.join()
Output of the application is as follows:
enter codstarting process p2
received ['p2', 0]
counter 1
starting process p1
received ['p1', 0]
counter 2
received ['p1', 2]
counter 3
...
counter 277
received ['p1', 332]
counter 278
received ['p2', -333]
Upvotes: 1
Views: 94
Reputation: 2999
You forgot to read from the pipe in the workers. And the pipe gets filled.
if conn.poll():
conn.recv() # consume messages!
x += seconds
Upvotes: 1