2

I am using perl to return data sets in XML. Now I have come across a situation where I need to run some clean up after sending a dataset to the client. But some where, in the chain of mod perl and Apache, the output gets held onto until my method returns.

I have attempted to clear the buffers with commands like.

$| =1;
STDOUT->flush(); # flush the buffer so the content is sent to the client and the finish hook can carry on, with out delaying the return.
if ($mod_perl_io){
    $mod_perl_io->rflush;
}

Yet I still get no output until my method returns. I then found out that my browser my be waiting for the connection to close and found that setting the content type in the header should fix this.

rint $cgi->header(-type => "text/plain; charset=UTF-8", -cookie => $config->{'cookie'});

Still no luck, in fact I had always been sending the correct headers.

So I though the best option is to simply start a new thread and let my method return. But when I create a new thread.

use threads ('yield',
             'stack_size' => 64*4096,
             'exit' => 'threads_only',
             'stringify');
my $thr = threads->create('doRebuild', $dbconnect, $dbusername, $dbpassword,  $bindir);
sub doRebuild {
my ($dbconnect, $dbusername, $dbpassword,  $bindir ) = @_;
1;
}

I get a segfault

[Fri Feb 22 10:16:47 2013] [notice] child pid 26076 exit signal Segmentation fault (11)

From what I have read this is done by mod perl to ensure thread safe operation. Not sure if this is correct.

So I thought I'd try using {exe }

{exec   'perl', "$bindir/rebuild_needed_values.pl", qw('$dbconnect' '$dbusername' '$dbpassword');}

From what I gather this is taking over the process from mod perl and not letting it return anything.

I know this isn't as specific as a question on stack overflow should be, but this sort of thing must be a common problem how have others solved it?

4

1 回答 1

1

您可以使用 fork(),但是我喜欢推荐http://gearman.org/进行后台处理。

像 Gearman 这样的解决方案要好得多,因为您的后台进程不在 Apache 的进程链中。

如果使用 gearman 实现,您的进程将在 Apache 重启后继续存在。它也更安全,因为 Gearman 环境可以在 chroot 监狱中运行。

使用 Gearman 的一个很好的副作用是您的后台进程可以从其他机器甚至其他语言调用。

Gearman 还可以在以后轻松地从您的流程中收集数据,并且您可以相当轻松地将进度信息反馈到您的 Web 应用程序。

于 2013-02-22T13:03:39.523 回答