Python I.P.C

Carl Banks imbosol at aerojockey.invalid
Thu Oct 9 20:14:31 EDT 2003


Daniel Greenblatt wrote:
> Basically, I have two python processes, a client and a server. 
> They are communicating via a socket. If the client can't connect to
> the server, it  starts a new server process, and has to wait until the
> server is up and listening for requests on the socket. Can anyone
> recommend a good way for the client to block until the server is ready
> to do business?


My recommendation: spawn the server, then sleep for a second, try to
connect, sleep, try to connect, ad connectionem.  It's simple, easy,
effective, robust, and most importantly, doesn't needlessly complicate
the server process.


But if you'd rather do it the hard way, probably the most straight-
forward way (in Unix) is to have the server send a signal (say,
SIGUSR1) to the client when it's ready, and have the client wait for
the signal.

Another possibility is to have the client open a pipe for reading,
passing the writing end to the server.  Have the server output
something (say, the string "READY") through the pipe when it's ready.
The client can wait for this string on its end.  I don't recommend
that, though, because it's a common and useful practice for a daemon
to close all open file descriptors when starting up.

See the Unix Programmer FAQ for details:
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16

One other thing to keep in mind: there is a race condition here.
Suppose a second clients starts up after a first clients spawns the
server, but before the server is initialized.  The second client would
also attempt to start the server, but (I think) that server will not
be able to listen to the same port.  If you don't take care to handle
this properly, it might leave the second client waiting for a signal
that'll never come.


-- 
CARL BANKS                   http://www.aerojockey.com/software

As the newest Lady Turnpot descended into the kitchen wrapped only in
her celery-green dressing gown, her creamy bosom rising and falling
like a temperamental souffle, her tart mouth pursed in distaste, the
sous-chef whispered to the scullery boy, "I don't know what to make of
her." 
          --Laurel Fortuner, Montendre, France 
            1992 Bulwer-Lytton Fiction Contest Winner




More information about the Python-list mailing list