gearman background后台job状态获取
2014-12-09 01:11 youxin 阅读(1021) 评论(0) 编辑 收藏 举报GearmanClient background job有一个方法叫:
public array GearmanClient::jobStatus ( string $job_handle
)
Get the status of a background job
返回值:
An array containing status information for the job corresponding to the supplied job handle. The first array element is a boolean indicating whether the job is even known, the second is a boolean indicating whether the job is still running, and the third and fourth elements correspond to the numerator and denominator of the fractional completion percentage, respectively.
example:Monitor the status of a long running background job
<?php /* create our object */ $gmclient= new GearmanClient(); /* add the default server */ $gmclient->addServer(); /* run reverse client */ $job_handle = $gmclient->doBackground("reverse", "this is a test"); if ($gmclient->returnCode() != GEARMAN_SUCCESS) { echo "bad return code\n"; exit; } $done = false; do { sleep(3); $stat = $gmclient->jobStatus($job_handle); if (!$stat[0]) // the job is known so it is not done $done = true; echo "Running: " . ($stat[1] ? "true" : "false") . ", numerator: " . $stat[2] . ", denomintor: " . $stat[3] . "\n"; } while(!$done); echo "done!\n"; ?>
问题是这个函数必须获得job_handle,在php官方文档里面找了很久没找到如何获得一个job_handle.
在stackoveflow看了很久,找到一个问题回答:
http://stackoverflow.com/questions/20563424/gearman-receive-data-of-processed-task
from irc, and looking at some python answers (http://stackoverflow.com/a/8349166/487878) I noticed that you could not get the data of a processed task from gearmand.
Either we want to store the data once it is processed by gearman worker and get the data back on the next call.
But you could not store the task handle and quit the client and try again with another client after the task is finished at gearman worker.
Before the worker finished you may get the response, but not after that. Hope that helps someone.
http://stackoverflow.com/questions/8344561/python-gearman-get-data-from-background-task
background tasks are called such because they allow the client that submitted them to un-block and work disconnected. They do not keep a channel of communication open to the client, so you won't get any of those status updates. They essentially go into the bit-bucket. If you want a background task to communicate its progress, you need to have some other channel for it to communicate with interested programs.
If you want the client to keep running and get updates, but not block on them, you can use the "task" method where you add a bunch of tasks, and then wait for any of them to provide status or be completed. I'm not sure if the pure python gearman interface has this, but the libgearman interface does. Its available in source form here https://launchpad.net/gearman-interface or in some versions of Ubuntu/Debian as python-gearman.libgearman.
一个比较好的回答:
http://stackoverflow.com/questions/11615941/gearman-is-there-still-no-way-to-retrieve-custom-data-from-a-background-worker
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通