最近我一直在研究 Beanstalkd 与 PHP 的使用。我已经学到了很多东西,但对服务器上的设置等有一些疑问。
以下是我如何看待它的工作原理:
- 我在我的 Ubuntu 服务器上安装 Beanstalkd 和任何依赖项(例如 libevent)。然后我启动 Beanstalkd 守护进程(它基本上应该始终运行)。
- 在我网站的某个地方(例如当用户执行某些操作时等)任务被添加到 Beanstalkd 队列中的各个管中。
-
我有一个 bash 脚本(例如下面的脚本),它作为守护进程运行,基本上执行 PHP 脚本。
<前><代码>#!/bin/sh
php 工人.php
4)工作脚本将有类似这样的东西来执行排队的任务:
while(1) {
$job = $this->pheanstalk->watch('test')->ignore('default')->reserve();
$job_encoded = json_decode($job->getData(), false);
$done_jobs[] = $job_encoded;
$this->log('job:'.print_r($job_encoded, 1));
$this->pheanstalk->delete($job);
}
现在这是我基于上述设置的问题(如果我错了,请纠正我):
-
假设我有导入的任务RSS 提要到数据库或其他东西中。如果 10 个用户同时执行此操作,他们都会在“测试”管中排队。然而,他们一次只会被处决一个。让 10 个不同的管同时执行会更好吗?
-
如果我确实需要更多管子,是否也意味着我需要 10 个工作脚本?每个管子都有一个,除了 watch() 函数中的字符串文字之外,都使用基本相同的代码同时运行。
-
如果我将该脚本作为守护进程运行,它是如何工作的?它会不断执行worker.php脚本吗?理论上,该脚本会循环直到队列为空,所以它不应该只启动一次吗?守护进程如何决定执行worker.php的频率?这只是一个设置吗?
谢谢!
Recently I've been researching the use of Beanstalkd with PHP. I've learned quite a bit but have a few questions about the setup on a server, etc.
Here is how I see it working:
- I install Beanstalkd and any dependencies (such as libevent) on my Ubuntu server. I then start the Beanstalkd daemon (which should basically run at all times).
- Somewhere in my website (such as when a user performs some actions, etc) tasks get added to various tubes within the Beanstalkd queue.
-
I have a bash script (such as the following one) that is run as a deamon that basically executes a PHP script.
#!/bin/sh
php worker.php
4) The worker script would have something like this to execute the queued up tasks:
while(1) {
$job = $this->pheanstalk->watch('test')->ignore('default')->reserve();
$job_encoded = json_decode($job->getData(), false);
$done_jobs[] = $job_encoded;
$this->log('job:'.print_r($job_encoded, 1));
$this->pheanstalk->delete($job);
}
Now here are my questions based on the above setup (which correct me if I'm wrong about that):
-
Say I have the task of importing an RSS feed into a database or something. If 10 users do this at once, they'll all be queued up in the "test" tube. However, they'd then only be executed one at a time. Would it be better to have 10 different tubes all executing at the same time?
-
If I do need more tubes, does that then also mean that i'd need 10 worker scripts? One for each tube all running concurrently with basically the same code except for the string literal in the watch() function.
-
If I run that script as a daemon, how does that work? Will it constantly be executing the worker.php script? That script loops until the queue is empty theoretically, so shouldn't it only be kicked off once? How does the daemon decide how often to execute worker.php? Is that just a setting?
Thanks!
发布评论
评论(1)
附录:
exec $@
重新运行它)。每当 php 脚本退出时,它都会重新运行 PHP。job-stat
command to see where a particular $job came from (which tube), or put some meta-information into the message if you need to tell each type from another.reserve()
to have it wait for a few seconds, or more, for the next job the become available without spinning out of control in a tight loop that does not pause at all - even if there was nothing to do.Addendum:
exec $@
). Whenever the php script exits, it re-runs the PHP.