mod_perl2/Perl 5 是否有相当于 PHP 的ignore_user_abort() 的函数?

发布于 2024-07-26 09:54:44 字数 275 浏览 3 评论 0原文

我正在编写一个内部服务,需要接触 mod_perl2 实例来实现长时间运行的进程。 该作业是通过 HTTP POST 触发的,mod_perl 处理程序会接收它并完成工作。 它可能需要很长时间,并且已准备好异步处理,因此我希望可以在 HTTP 连接运行时终止它。

PHP 有一个函数 ignore_user_abort(),当与正确的标头结合使用时,可以提前关闭 HTTP 连接,同时保持进程运行(这里多次提到此技术)。

Perl 有等效的吗? 我还没找到。

I'm writing an internal service that needs to touch a mod_perl2 instance for a long-running-process. The job is fired from a HTTP POST, and them mod_perl handler picks it up and does the work. It could take a long time, and is ready to be handled asynchronously, so I was hoping I could terminate the HTTP connection while it is running.

PHP has a function ignore_user_abort(), that when combined with the right headers, can close the HTTP connection early, while leaving the process running (this technique is mentioned here on SO a few times).

Does Perl have an equivalent? I haven't been able to find one yet.

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

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

发布评论

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

评论(2

饭团 2024-08-02 09:54:44

好吧,我明白了。

Mod_perl 在这里有 PHP 的“相反”问题。 默认情况下,mod_perl 进程保持打开状态,即使连接中止也是如此,而 PHP 默认情况下会关闭该进程。

实用 mod_perl 书说明了如何处理中止连接。

(顺便说一句,就这个特定问题而言,作业队列在列表中的位置低于“断开连接”的 http 进程)

#setup headers
$r->content_type('text/html');
$s = some_sub_returns_string();

$r->connection->keepalive(Apache2::Const::CONN_CLOSE);
$r->headers_out()->{'Content-Length'} = length($s);

$r->print($s);
$r->rflush();

#
# !!! at this point, the connection will close to the client
#

#do long running stuff
do_long_running_sub();

Ok, I figured it out.

Mod_perl has the 'opposite' problem of PHP here. By default, mod_perl processes are left open, even if the connection is aborted, where PHP by default closes the process.

The Practical mod_perl book says how to deal with aborted connections.

(BTW, for the purposes of this specific problem, a job queue was lower on the list than a 'disconnecting' http process)

#setup headers
$r->content_type('text/html');
$s = some_sub_returns_string();

$r->connection->keepalive(Apache2::Const::CONN_CLOSE);
$r->headers_out()->{'Content-Length'} = length($s);

$r->print($s);
$r->rflush();

#
# !!! at this point, the connection will close to the client
#

#do long running stuff
do_long_running_sub();
扬花落满肩 2024-08-02 09:54:44

您可能想考虑使用作业队列来实现此目的。 这是 Zend 提供的一个,它可以让您启动后台处理作业。 php 和 perl 应该有很多可供选择的。

这是另一个讨论这个问题和关于一些 php 选项的文章。 我不是 Perl 僧侣,所以我会给其他人留下关于这些工具的建议。

You may want to look at using a job queue for this. Here is one provided by Zend that will let you start background processing jobs. There should be a number of these to choose from for php and perl.

Here's another thread that talks about this problem and an article on some php options. I'm not perl monk, so I'll leave suggestions on those tools to others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文