2

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()

user1934513
  • 693
  • 4
  • 21
  • hypothesis: multiprocessing is probably not playing nice with Gevent. – bombela Sep 13 '18 at 17:04
  • zerorpc usage: you should return the pong directly from the ping method, after having waited for the background task to terminate. You will get the benefit of heartbeat and timeout managed for you by zerorpc. – bombela Sep 13 '18 at 17:07

1 Answers1

0

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.

Skinner927
  • 953
  • 2
  • 13
  • 25