如何将 GearmanJob 与 GearmanWorker 和 GearmanClient 联系起来?
public GearmanTask GearmanClient::addTask ( string $function_name , string $workload [, mixed &$context [, string $unique ]] ) public bool GearmanWorker::addFunction ( string $function_name , callback $function [, mixed &$context [, int $timeout ]] )
这些是可用于集成两者的类方法。 有了这些,您如何将工作负载与被调用的函数联系起来?
public GearmanTask GearmanClient::addTask ( string $function_name , string $workload [, mixed &$context [, string $unique ]] ) public bool GearmanWorker::addFunction ( string $function_name , callback $function [, mixed &$context [, int $timeout ]] )
These are class methods that can be used to integrate the two.
With these, how do you relate the workload with the called function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GearmanClient
用于提交任务。通常,这是通过网页或用于读取要提交的任务列表的脚本来完成的。GearmanWorker
的设置方式是让许多并行“工作线程”可以同时运行。从逻辑上讲,工人所做的任何事情都应该代表单个原子工作单元。这可能意味着将一个对象转换为另一个对象并将其保存回数据库,或者为单个用户组装并发送 HTML 电子邮件。$function_name
是一个采用单个参数的函数,该参数是一个GearmanJob
对象。因此,您的控制器脚本可能如下所示。
然后,你的工人会做这样的事情。
需要记住的一些事情:
while
循环。确保正确设置超时和内存限制。exit;
PHP 的内存使用情况仍然很糟糕,并且很难判断工作程序何时会因为内存不足而被杀死。GearmanClient
is used to submit a task. Normally this is done from a web page, or a script meant to read a list of tasks to be submitted.GearmanWorker
is meant to be set up in such a way that many parallel 'workers' can be run at the same time. Logically, whatever a worker does should represent a single atomic unit of work. That can mean doing a single transformation of an object into another and saving it back to the database, or assembling and sending an html email for a single user.$function_name
is a function that takes a single argument, which is aGearmanJob
object.So, your controller script might look something like this.
Then, your worker will do something like this.
A few things to keep in mind:
while
loop. Make sure to set your timeout and memory limits appropriately.exit;
PHP still sucks with memory usage, and it can be hard to tell when a worker will be killed because it ran out of memory.