lang2
lang2

Reputation: 11966

Multiple socket monitoring

I'm designing a python program that'll talk to two other process at the same time through sockets. One of the process is a C daemon so this socket will be alive all the time - no problem there. The other process is a PHP web page. So that socket isn't established all the time. Most of the time, the socket is listen()ing on a port.

If both socket are alive all the time, a simple select() call can be used to monitor input from both. But in my situation, this is not possible. How can I achieve this easily?

Thanks,

Upvotes: 0

Views: 337

Answers (1)

jd.
jd.

Reputation: 10958

You can use select() in this case, even in a single-threaded single-process program with only blocking sockets. Here's how you would accept incoming connections with select():

daemonSocket = socket.socket()
...
phpListenSocket = socket.socket()
phpListenSocket.bind(...)
phpListenSocket.listen(...)
phpSocket = None

while True:
    rlist = ...
    rready, wready, eready = select(rlist, [], [])
    if phpListenSocket in rready:
        phpSocket, remoteAddr = phpListenSocket.accept()

Upvotes: 2

Related Questions