广播式IPC
考虑到让进程或线程相互交互的所有选项(锁、互斥体、信号量、消息队列、共享内存等),我有点迷失了如何最好地做我想做的事情。
我想要多个进程等待某个事件的发生。也就是说,我希望它们阻塞直到
- 达到某个超时,或者
- 另一个进程触发某个事件。
在任何时候,可以有任意数量的此类等待进程,并且当唤醒事件发生时,所有这些进程都必须被唤醒,而不仅仅是一个。
可能使这变得更加困难的一个限制是:它必须是 PHP,并且还必须在 apache 中运行的 mod_php 上工作。
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
- a certain timeout is reached, or
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,这有点“hacky”,但你可以用套接字来做到这一点。这实际上取决于你想要做什么......我真的想知道你是否真的需要这种系统(而不是试图将流程简化到根本不需要 IPC)......
创建一个“监听器”守护进程,除了接受套接字连接并将它们放入队列之外什么都不做。它将运行
socket_select
等待用于新连接或要写入套接字的数据。如果数据已写入,它将将该数据写入其所有活动连接,然后关闭它们并重新开始。如果收到新连接,它会将其放入队列中,然后返回选择...因此,在您的“子”中,您所需要做的就是连接到主服务器,设置阻塞
socket_set_block($sock )
,然后设置超时:然后,只需从套接字读取 (
socket_read($sock)
)。它会阻塞直到您设置的超时时间或直到“主”写回它为止。通过该调用后,只需关闭套接字并继续做您想做的事情......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: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...