Pheanstalk(beanstalk 的 PHP 客户端)- 连接如何工作?
我需要一些帮助来理解 pheanstalk (php beanstalk 客户端)的使用。我有一个 PHP 程序,当表单数据发送到服务器时,该程序在服务器上执行。然后,PHP 程序应将表单数据打包为 JSON 结构并将其发送到后端服务器进程。
我不明白的是与 beanstalkd 服务器的连接。我是否应该在每次 PHP 程序执行时创建一个新的 Pheanstalk() 对象 - 在这种情况下,我是否会产生创建连接的成本。连接何时关闭(因为 pheanstalk 中没有 close() 方法)?
如果连接是持久的,它是否在 PHP 程序的所有执行之间共享,在这种情况下,并发命中时会发生什么情况?感谢您的任何帮助。
I'd like some help understanding the use of pheanstalk (php beanstalk client). I have a PHP program that is executed on the server when form data is sent to it. The PHP program should then package the form data as a JSON structure and send it to a backend server process.
The thing I don't understand is the connection to the beanstalkd server. Should I create a new Pheanstalk() object each time the PHP program executes - in which case, am I incurring the cost of creating the connection. When is the connection closed (since there is no close() method in pheanstalk)?
If the connection is persistent, is it shared among all executions of the PHP program, in which case, what happens in the case of concurrent hits? Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,每次启动程序时,您都必须与 Pheanstalk(或任何其他库)创建一个新连接,因为 PHP 会重新启动每个连接。不过开销很小。
Beanstalkd 进程经过优化,可以轻松处理大量连接,并且会以原子方式对它们进行操作 - 你不会得到重复的作业,除非你将两个相同的作业放在那里(即使这样,它们也会有不同的作业 ID) )。
在发送第一个命令之前,Pheanstalk 甚至不会向守护进程发送任何信息(包括打开连接)。正是由于这个原因,除非您主动发出请求,否则您无法判断守护进程是否还活着(在我的测试中,我获得了当前管的列表)。如果您在运行的程序中继续重复使用实例化的类,那么它当然会继续重复使用它。
没有正式的
close()
,但是unset($pheanstalk)
会做同样的事情,运行析构函数。同样,该调用是程序如此短暂,并且如果允许,守护进程可以保持如此多的并发连接打开,这不是问题 - 并且它将像程序本身一样关闭。简而言之,不用担心。连接 Beanstalkd 以及将数据发送到 Beanstalkd 或从 Beanstalkd 发出的开销可能只是工作线程或生产者在生成请求/响应时所做的任何工作的一小部分。
Yes, you will have to create a new connection with Pheanstalk (or any other library) each time you start the program, since PHP starts each one fresh. The overhead is tiny though.
The Beanstalkd process is optimised to easily handle a number of connections, and will act on them atomically - you won't get a duplicate job, unless you put two of the same in there (and even then, they would have different job-ID's).
Pheanstalk doesn't even send data to the daemon any information (including opening the connection) until the first command is sent. It's for this reason that you can't tell if the daemon is even alive till you actively make a request (in my tests, I get the list of current tubes). If you kept re-using the instantiated class in the running program, then it would keep reusing it of course.
There's no formal
close()
, butunset($pheanstalk)
would do the same thing, running the destructor. Again, the call is program so transient and the daemon can keep so many concurrent connections open if it's allowed to, that it's not an issue - and it will be shut down as the program itself does.In short, don't worry. The overhead of connecting and sending data into, or out of, Beanstalkd will probably be a tiny fraction of any work that is done by the worker, or producer, in generating the request/response.