Python I.P.C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Greenblatt

    Python I.P.C

    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
  • Carl Banks

    #2
    Re: Python I.P.C

    Daniel Greenblatt wrote:[color=blue]
    > 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?[/color]


    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:


    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

    Comment

    • Daniel Dittmar

      #3
      Re: Python I.P.C

      Daniel Greenblatt wrote:[color=blue]
      > 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?[/color]

      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



      Comment

      Working...