如何编写在后台运行的异步Web服务php

发布于 2024-12-02 17:47:35 字数 210 浏览 1 评论 0原文

我正在使用 php 编写后台处理 Web 服务,即使用户关闭浏览器也需要在后台运行,有什么方法可以使用 php 来做到这一点吗?

更多信息

是的,我正在使用 symfony/php 从事大型 Web 项目(工资单)。它需要在每个月工资用户来并单击处理按钮时进行处理。那么工资单应该在没有 apache 服务器超时的情况下处理。为此,我希望编写在后台运行的异步 Web 服务。

im up to write background processing web service using php that need to run in background even user closed the browser is there any way to do this using php ?

More Info

Yes im working on large web project (pay roll) using symfony/php. it needs to process in every month when payroll user comes and click the process button. then payroll should process without apache server time out. for do that i hope to wirte asynchronous web service that run in background.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈下烟灰 2024-12-09 17:47:35

正如评论者所说,您应该使用 CRON 作业,因为它最适合此类问题。但是,您需要通过用户单击来启动作业。这是我要使用的:

  1. 在用户单击时,在某个表中创建一行,或创建一个文件
    具有执行任务所需的参数。基本上它说的是
    CRON“嘿,你需要启动任务”。
  2. 设置一个 CRON 作业来每分钟处理该行/文件,并且
    如果找到行/文件,则启动任务。 删除之前的文件
    启动任务,或者可能发生并行任务。
  3. 如果您需要在工资单完成时告诉用户,请使
    任务创建另一个行/文件以显示 CRON 结束,并使用
    javascript 每 30 秒/1 分钟刷新一次用户页面,并停止
    找到新行/文件时自动刷新,并显示
    适当的输出/通知消息。

As commentor said, you should use a CRON job as it's best suited for this kind of problems. However, you need to launch your job on the click of a user. Here is what I'd use:

  1. On the click of a user, create a row in some table, or create a file
    with needed parameter to execute the task. Basically it says to the
    CRON 'hey, you need to launch the task'.
  2. Setup a CRON job to look after that row/file every minute, and
    launch the task if the row/file is found. Delete the file before
    launching the task, or parallels tasks could happen.
  3. If you need to tell your user when the payroll is done, make the
    task create another row/file to show that the CRON ended, and with
    javascript refresh your user page every 30s/1min, and stop the
    automatic refresh when the new row/file is found, and display the
    appropriate output/notice message.
南城追梦 2024-12-09 17:47:35

看看这篇文章 。根据您正在执行的操作,这可能比 CRON 作业好得多,特别是如果您想立即采取行动。 CRON 作业最多只能运行一次,每分钟一次,通过这种方法,您可以立即开始在后台处理请求。

// this script can run forever
set_time_limit(0);
 
// tell the client the request has finished processing
header('Location: index.php');  // redirect (optional)
header('Status: 200');          // status code
header('Connection: close');    // disconnect
 
// clear ob stack 
@ob_end_clean();
 
// continue processing once client disconnects
ignore_user_abort();
 
ob_start();
/* ------------------------------------------*/
/* this is where regular request code goes.. */
 
/* end where regular request code runs..     */
/* ------------------------------------------*/
$iSize = ob_get_length();
header("Content-Length: $iSize");
 
// if the session needs to be closed, persist it
// before closing the connection to avoid race
// conditions in the case of a redirect above
session_write_close();
 
// send the response payload to the client
@ob_end_flush();
flush();
 
/* -------------------------------------------*/
/* code here runs after the client disconnect */

Take a look at this article. Depending on what you're doing this can be much better than a CRON job, most specifically, if you want to take action immediately. CRON jobs are limited to running at most, once per minute, with this approach, you can begin handling the request in the background immediately.

// this script can run forever
set_time_limit(0);
 
// tell the client the request has finished processing
header('Location: index.php');  // redirect (optional)
header('Status: 200');          // status code
header('Connection: close');    // disconnect
 
// clear ob stack 
@ob_end_clean();
 
// continue processing once client disconnects
ignore_user_abort();
 
ob_start();
/* ------------------------------------------*/
/* this is where regular request code goes.. */
 
/* end where regular request code runs..     */
/* ------------------------------------------*/
$iSize = ob_get_length();
header("Content-Length: $iSize");
 
// if the session needs to be closed, persist it
// before closing the connection to avoid race
// conditions in the case of a redirect above
session_write_close();
 
// send the response payload to the client
@ob_end_flush();
flush();
 
/* -------------------------------------------*/
/* code here runs after the client disconnect */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文