user1934513
user1934513

Reputation: 725

python zerorpc and multiprocessing issue

I'm implementing a bi-directional ping-pong demo app between an electron app and a python backend.

This is the code for the python part which causes the problems:

import sys
import zerorpc
import time
from multiprocessing import Process

def ping_response():
    print("Sleeping")
    time.sleep(5)
    c = zerorpc.Client()
    c.connect("tcp://127.0.0.1:4243")
    print("sending pong")
    c.pong()

class Api(object):    
    def echo(self, text):
        """echo any text"""
        return text

    def ping(self):

        p = Process(target=ping_response, args=())
        p.start()
        print("got ping")
        return

def parse_port():
    port = 4242
    try:
        port = int(sys.argv[1])
    except Exception as e:
        pass
    return '{}'.format(port)

def main():
    addr = 'tcp://127.0.0.1:' + parse_port()
    s = zerorpc.Server(Api())
    s.bind(addr)
    print('start running on {}'.format(addr))
    s.run()

if __name__ == '__main__':
    main()

Each time ping() is called from javascript side it will start a new process that simulates some work (sleeping for 5 seconds) and replies by calling pong on nodejs server to indicate work is done.

The issue is that the pong() request never gets to javascript side. If instead of spawning a new process I create a new thread using _thread and execute the same code in ping_response(), the pong request arrives in the javascript side. Also if I manually run the bash command zerorpc tcp://localhost:4243 pong I can see that the pong request is received by the nodejs script so the server on the javascript side works ok.

What happens with zerorpc client when I create a new process and it doesn't manage to send the request ?

Thank you.

EDIT It seems it gets stuck in c.pong()

Upvotes: 2

Views: 562

Answers (1)

Skinner927
Skinner927

Reputation: 980

Try using gipc.start_process() from the gipc module (via pip) instead of multiprocessing.Process(). It creates a new gevent context which otherwise multiprocessing will accidentally inherit.

Upvotes: 0

Related Questions