views:

44

answers:

1

Given all the options to have processes or threads interact with each other (locks, mutex, semaphores, message queues, shared memory, etc), I'm a bit lost about what's best to do what I want.

I want several processes to wait for a certain event to happen. That is, I want them to block either until

  1. a certain timeout is reached, or
  2. a certain event is triggered by another process.

At any time, there can be an arbitrary number of such waiting processes and when the wake-up event happens, all of them must wake up, not just one.

And the one restriction that probably makes this much harder is: It has to be PHP and it must also work from mod_php running in apache.

A: 

Well, it's a bit "hacky", but you could do this with sockets. It really depends on what you're trying to do... I really wonder if you actually need this kind of system (rather than trying to simplify the processes to where they don't need IPC at all)...

Create a "listener" deamon that does nothing but accept socket connections and put them in a queue. It would run socket_select waiting for either a new connection, or data to be written to the sockets. If data was written, it writes that data to all of its active connections then closes them and starts over. If a new connection is received, it puts it in the queue and then goes back to selecting...

So then in your "child", all you need to do is connect to the master, set blocking socket_set_block($sock), and then set your timeout:

socket_set_option(
    $sock,
    SOL_SOCKET,  // socket level
    SO_SNDTIMEO, // timeout option
    array(
        "sec"=>10, // Timeout in seconds
        "usec"=>0  // I assume timeout in microseconds
    )
);

Then, simply read from the socket (socket_read($sock)). It'll block for up to the timeout you set or until the "master" writes back to it. After passing that call, just close the socket and continue on doing what you want to do...

ircmaxell
This sounds incredibly interesting... let me check on that.
jjj
Again, I would like to stress that if I were you I would try to re-model the application to not require any IPC if possible first. This will likely work for the question you asked (And doesn't have the infinite blocking issues of using other forms of IPC such as semaphores), but I'd try to find a normal solution first before implementing an IPC system such as this...
ircmaxell
Yes, sorry for not giving more details. It will be a web-application where a long AJAX query will first hang and wait and then return at a precise moment. And that return must be synchronized with all users viewing that webpage. Possibly, a console version of the application will be added too.
jjj
Ok, I implemented that and it works great! It's a simple echo-daemon which accepts connections on a UNIX domain socket; whenever it receives data on any connection, it broadcasts it to all other connections. So all listener can connect and wait for data (with a timeout) and the event triggerer can connect and send some data and then close again. Thanks a lot!
jjj
Not a problem! Glad it could help. And considering that you're not using TCP it should be reasonably efficient... Just beware that leaving web connections blocking can cripple your server if you get a high request rate (Especially if using mod_php)...
ircmaxell