0

In my python program I am trying to use rpyc library to start a server as a separate process. As soon as I do that, I want to be able to connect to that server. Since it takes some time for the process to start, I need to delay connecting to it. I need some suggestions on what is the best way to do this.

Currently I am using time.sleep(10) after starting the process. The assumption is that 10 seconds ought to be enough for the server process to start and get ready to accept requests. This is definitely not foolproof.

Aviral Goel
  • 308
  • 1
  • 3
  • 13
  • 1
    duplicate? [synchronization across multiple processes in python](https://stackoverflow.com/questions/16654908/synchronization-across-multiple-processes-in-python) On a separate note, if you have a 'redis' database lying around then using the 'blocking' list 'pop' command can also be used. [BLPop](http://redis.io/commands/blpop). And is also an easy way to transfer information between them. Just a thought. – Ryan Vincent Feb 11 '15 at 10:49

1 Answers1

0

You might like to use gearman. Here you can keep worker binded on particular port as a server process. And make request from gearman client.

As gearman worker would pretend like daemon process, as soon as it receives request, it responds instantly :

gearman worker (server for you):

import gearman

def check_request_status(job_request):
    if job_request.complete:
        print "Job %s finished!  Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result)
    elif job_request.timed_out:
        print "Job %s timed out!" % job_request.unique
    elif job_request.state == JOB_UNKNOWN:
        print "Job %s connection failed!" % job_request.unique

gm_client = gearman.GearmanClient(['localhost:4730'])

completed_job_request = gm_client.submit_job("reverse", "Hello World!")
check_request_status(completed_job_request)

gearman client:

import gearman

gm_worker = gearman.GearmanWorker(['localhost:4730'])

def task_listener_reverse(gearman_worker, gearman_job):
    print 'Reversing string: ' + gearman_job.data
    return gearman_job.data[::-1]

# gm_worker.set_client_id is optional
gm_worker.set_client_id('python-worker')
gm_worker.register_task('reverse', task_listener_reverse)

# Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
gm_worker.work()
user123
  • 5,269
  • 16
  • 73
  • 121