简体   繁体   English

使用Perl的Dancer分叉在子进程中运行代码

[英]Forking to Run Code in a Child Process With Perl's Dancer

I have a Dancer app in perl that handles get/post requests. 我在perl中有一个处理get / post请求的Dancer应用程序。

One of my example routes is below: 我的一个示例路线如下:

post '/handle_data' => sub {

    # perform some calculations
    ...
    # store some data
    ...
    # do some long running tasks
    ...
};

My question: is there an easy way to do the long running tasks in another process without blocking the request? 我的问题:是否有一种简单的方法可以在不阻止请求的情况下在另一个进程中执行长时间运行的任务

I've tried fork and return in the parent process, with an exit after the long running tasks for the child, but that all seems to block the response from being sent until the child completes. 我已经在父进程中尝试fork and return ,在子进程的长时间运行任务之后exit ,但是这一切似乎都阻止了在子进程完成之前发送的响应。

Any help is appreciated. 任何帮助表示赞赏。

EDIT: 编辑:

I ended up switching to a full-fledged job server, specifically the perl version of Gearman 我最终切换到一个成熟的作业服务器,特别是Gearman的perl版本

the fork and return appears to work for me. fork和return似乎对我有用。

I tried: 我试过了:

#!/usr/bin/perl

use Dancer;

get '/' => sub {
    fork and return "Content!\n";

    sleep 10; # do your actual work here
    warn "Child process done\n";
    exit;
};

dance;

Running that via the standalone server, or via plackup/Starman, works as expected - the "Content!\\n" is returned to the user's browser immediately, and the request is over; 通过独立服务器或通过plackup / Starman运行它,按预期工作 - “Content!\\ n”立即返回给用户的浏览器,请求结束; the forked process hangs around for ten seconds sleeping, then issues the warning (visible on the terminal) then exits. 分叉的过程会暂停十秒钟,然后发出警告(在终端上可见)然后退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM