如何PHP的客户端和C守护服务器之间的IPC? [英] How to IPC between PHP clients and a C Daemon Server?

查看:188
本文介绍了如何PHP的客户端和C守护服务器之间的IPC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和感谢在这个问题考虑看看。

and thanks for taking a look at the question.

背景结果
我有几台机器在很短的时间内连续产生多个(最多300),PHP脚本的控制台。这些脚本快速运行(不到一秒),然后退出。所有这些脚本的需要只读到大线索的结构访问这将是非常昂贵的,每次加载到存储器每一个脚本的运行。服务器运行Linux操作系统。

The background
I have several machines that continuously spawn multiple (up to 300) PHP console scripts in a very short time frame. These scripts run quickly (less than a second) and then exit. All of these scripts need read only access to a large trie structure which would be very expensive to load into memory each time each one of the scripts runs. The server runs Linux.

我的解决方案结果
创建一个C守护程序,保持在存储器中的线索结构,并接收从PHP客户端的请求。这将收到来自PHP的每一位客户的要求,对存储结构进行查找,并与答案回应,从做这项工作节省了PHP脚本。请求和响应是短字符串(不超过20个字符)

My solution
Create a C daemon that keeps the trie structure in memory and receives requests from the PHP clients. It would receive a request from every one of the PHP clients, perform the lookup on the memory structure and respond with the answer, saving the PHP scripts from doing that work. Both requests and responses are short strings (no longer than 20 characters)

我的问题结果
我很新的C守护程序和进程间通信。经过反复研究,我已经缩小了选择到消息队列和Unix域套接字。因为我觉得(我可能是错的),他们排队所有的守护进程的请求以串行方式回答消息队列显得足够。 unix域套似乎更容易使用,虽然。不过,我有各种各样的问题我一直没能找到答案:

My problem
I am very new to C daemons and inter process communication. After much research, I have narrowed the choices down to Message Queues and Unix domain sockets. Message Queues seem adequate because I think (I may be wrong) that they queue up all of the requests for the daemon to answer them serially. Unix domain sockets seem to be easier to use, though. However, I have various questions I have not been able to find answers to:


  1. 如何在PHP脚本发送和接收邮件,或使用UNIX插座与守护进程进行沟通?相反怎样的C守护进程跟踪哪些PHP程序的它发送给一个答复?

  2. 我见过用无限的,而里面休眠状态循环守护进程的大多数例子。我的守护进程需要服务,可以随时来很多连接,并且响应延迟是至关重要的。将如何反应守护如果PHP脚本发送,而它正在睡觉的请求?我看了一下poll和epoll的,这会是等待接收的消息的正确方法是什么?

  3. 每个PHP进程将始终发送一个请求,然后等待接收响应。我需要确保,如果守护程序关闭/不可用,PHP进程将等待一组最大时间的响应,如果没有答案接收将继续挂无论代替。可以这样做?

数据结构的实际查找是非常快的,我不需要任何复杂的多线程或类似的解决方案,因为我相信处理以FIFO方式的要求就足够了。我还需要保持简单愚蠢的,因为这是一个关键任务服务,我是相当新的这种类型的节目。 (我知道,但我真的没有办法解决这个,和学习经验将是伟大的)

The actual lookup of the data structure is very fast, I don't need any complex multi-threading or similar solution, as I believe handling the requests in a FIFO manner will be enough. I also need to keep it simple stupid, as this is a mission critical service, and I am fairly new to this type of program. (I know, but I really have no way around this, and the learning experience will be great)

我真的AP preciate code片段,照一些光成,我有具体问题。链接指南和指针,这将进一步我的理解变为低电平IPC的这个阴暗世界也欢迎。

I would really appreciate code snippets that shine some light into the specific questions that I have. Links to guides and pointers that will further my understanding into this murky world of low level IPC are also welcome.

感谢您的帮助!

现在知道更多比我在问这个问题的时候,我只是想指出,任何人有兴趣,无论是节俭框架和 ZeroMQ 做抽象掉硬盘,插座级编程的出色的工作。节俭,甚至给你的服务器免费脚手架!

Knowing much more now than I did at the time of asking this question, I just wanted to point out to anybody interested that both the Thrift framework and ZeroMQ do a fantastic job of abstracting away the hard, socket-level programming. Thrift even gives you the scaffolding for the server for free!

在事实上,而不是去建立一个网络服务器的所有的辛勤工作,只考虑编写您使用已经解决了这个问题你不错的异步服务器应用程序服务器code。使用异步IO当然服务器,非常适合不需要密集的CPU处理(或者事件循环块)的网络应用。

In fact, instead of going to all the hard work of building a network server, consider just writing you applications server code using a good asynchronous server that has already solved the problem for you. Of course servers that use asynchronous IO are great for network applications that don't require intensive CPU processing (or else the event loop blocks).

对于Python的例子:扭曲 GEVENT 。我preFER GEVENT,因为它是专注于HTTP服务器端我不包括龙卷风。

Examples for python: Twisted, gevent. I prefer gevent, and I don't include tornado because it is focused on the HTTP server side.

有关Ruby的例子: EventMachine的

Examples for Ruby: EventMachine

如果你想要去更深,阅读 C10K问题和的 Unix网络编程

If you want to go deeper, read the C10k Problem, and Unix Network Programing.

推荐答案

我怀疑节俭是什么你要。你必须写一点胶水code做PHP< -thrift-> C ++< - > C,但是这可能会比滚动自己更强大的

I suspect Thrift is what you want. You'd have to write a little glue code to do PHP <-thrift-> C++ <-> C, but that would probably be more robust than rolling your own.

这篇关于如何PHP的客户端和C守护服务器之间的IPC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆