如何分叉Perl CGI程序来解决长时间运行的任务?

时间:2023-01-15 20:46:46

I am writing a Bulk Mail scheduler controlled from a Perl/CGI Application and would like to learn abut "good" ways to fork a CGI program to run a separate task? Should one do it at all? Or is it better to suffer the overhead of running a separate job-queue engine like Gearman or TheSchwartz as has been suggested recently. Does the answer/perspective change when using an near-MVC framework like CGI::Application over vanilla CGI.pm? The last comes from a possible project that I have in mind for a CGI::Application Plugin - that would make "forking" a process relatively simple to call.

我正在编写一个由Perl / CGI应用程序控制的批量邮件调度程序,并希望学习“好”方法来分叉CGI程序来运行单独的任务?一个人应该这样做吗?或者最好是像最近建议的那样,承受运行像Gearman或TheSchwartz这样的单独作业队列引擎的开销。当使用像CGI :: Application这样的近MVC框架而不是vanilla CGI.pm时,答案/透视是否会改变?最后一个来自一个可能的项目,我想到的CGI :: Application插件 - 这将使“分叉”一个过程相对简单的调用。

2 个解决方案

#1


Look at Proc::Daemon - it's the simplest thing that works. From your CGI script, do the CGI business (getting input, returning a response to the browser), then call Proc::Daemon::init() which does the fork, daemonizes your process and makes the parent exit. Then your script (now a daemon) does its long-running tasks and exits when they're done. You'll want to update something (file, database record) while running as a daemon, so subsequent CGI invocations can check what it did (or how it's progressing).

看看Proc :: Daemon - 这是最简单的方法。从您的CGI脚本,执行CGI业务(获取输入,返回对浏览器的响应),然后调用Proc :: Daemon :: init()执行fork,守护进程并使父进程退出。然后你的脚本(现在是一个守护进程)执行其长期运行的任务,并在完成后退出。您希望在作为守护程序运行时更新某些内容(文件,数据库记录),因此后续的CGI调用可以检查它的作用(或它的进展情况)。

#2


Would something like POE be useful? It's more event-driven than forked, but it may meet your needs.

像POE这样的东西会有用吗?它比分叉更受事件驱动,但它可能满足您的需求。

#1


Look at Proc::Daemon - it's the simplest thing that works. From your CGI script, do the CGI business (getting input, returning a response to the browser), then call Proc::Daemon::init() which does the fork, daemonizes your process and makes the parent exit. Then your script (now a daemon) does its long-running tasks and exits when they're done. You'll want to update something (file, database record) while running as a daemon, so subsequent CGI invocations can check what it did (or how it's progressing).

看看Proc :: Daemon - 这是最简单的方法。从您的CGI脚本,执行CGI业务(获取输入,返回对浏览器的响应),然后调用Proc :: Daemon :: init()执行fork,守护进程并使父进程退出。然后你的脚本(现在是一个守护进程)执行其长期运行的任务,并在完成后退出。您希望在作为守护程序运行时更新某些内容(文件,数据库记录),因此后续的CGI调用可以检查它的作用(或它的进展情况)。

#2


Would something like POE be useful? It's more event-driven than forked, but it may meet your needs.

像POE这样的东西会有用吗?它比分叉更受事件驱动,但它可能满足您的需求。