Python I.P.C

D

Daniel Greenblatt

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?

Thanks
Dan
 
C

Carl Banks

Daniel said:
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
 
D

Daniel Dittmar

Daniel said:
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?

Create a server socket in the client, spawn the server (pass the socket
port), then listen on the server socket until the server connects to it.

The you can remove this socket and start the regular program.

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,903
Members
47,444
Latest member
Michaeltoyler01

Latest Threads

Top