如何派生一个Perl CGI程序来完成长时间运行的任务? [英] How can I fork a Perl CGI program to hive off long-running tasks?

查看:48
本文介绍了如何派生一个Perl CGI程序来完成长时间运行的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个由Perl / CGI应用程序控制的Bulk Mail调度程序,并且想学习一种好的方式来派生CGI程序来运行单独的任务?一个人应该做吗?还是忍受运行单独的作业队列引擎(例如 Gearman )的开销或 TheSchwartz 一个href = https://stackoverflow.com/questions/942749/scheduling-one-time-tasks-from-a-perl-cgi-application title =最近建议>最近建议。使用像CGI :: Application之类的近MVC框架而不是普通CGI.pm时,答案/观点是否会改变?最后一个来自我可能想到的CGI :: Application插件项目,它将使派生过程的调用相对简单。

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.

推荐答案

查看Proc :: Daemon-这是最简单的方法。在您的CGI脚本中,进行CGI业务(获取输入,将响应返回到浏览器),然后调用Proc :: Daemon :: init()进行派生,守护进程并退出父进程。然后,您的脚本(现在是守护程序)执行其长期运行的任务,并在完成后退出。
您需要在作为守护程序运行时更新某些内容(文件,数据库记录),以便随后的CGI调用可以检查其执行的操作(或执行情况)。

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).

这篇关于如何派生一个Perl CGI程序来完成长时间运行的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆