You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Aku Kauste <ak...@gmail.com> on 2009/10/02 15:34:17 UTC

Threading inside a handler script

Hello,

The problem I'm trying to solve is that a page request will have to
look for data from two sources: local & external. What I would like to
do is put the external lookup (which is slow) in it's own thread and
do the local processing as far as I can while waiting for the external
source. Then join the thread and do what I have to do before returning
the page.

Well, obviously this doesn't work and I end up getting "child pid
XXXXX exit signal Segmentation fault (11)" type errors.

Is my aproach completely wrong? What would be the right way to solve
this kind of a problem?

I'm trying to run this under ModPerl::Registry handler.iIn Debian
Lenny server with Apache 2.2.9, Perl 5.10.0, mod_perl 2.0.4. I've
tested with different MPM's (debian packages: apache2-mpm-prefork,
apache2-mpm-worker, apache2-mpm-event)

I've simplified my test script to:

#!/usr/bin/perl

use strict;
use warnings;

use threads;

my $t = async { sleep 2; return 123; };

print "Content-type: text/html\n\n";

my $ret = $t->join;
print "<h1>Got: $ret</h1>";

__END__

- Aku

Re: Threading inside a handler script

Posted by Torsten Foertsch <to...@gmx.net>.
On Sat 03 Oct 2009, Elizabeth Mattijsen wrote:
> > I believe what you're doing should work, but I don't use threads
> > for anything.  I'd probably fork and put the results in a database
> > table or file.
>
> If you like the threads API, you might want to take a look at the  
> "forks" module on CPAN.  It provides the threads API using fork(),
> and   hence does not have many of the problems that native Perl
> ithreads have.

Or Coro. I have never used it with modperl but I think it will work. It 
does not use kernel threads but implements perl stack switches. So, you 
have to watch out to avoid blocking operations. Similar to "win 3.x 
multitasking" or early UNIX threading approaches.

Torsten

-- 
Need professional mod_perl support?
Just hire me: torsten.foertsch@gmx.net

Re: Threading inside a handler script

Posted by Aku Kauste <ak...@gmail.com>.
Thanks Liz,

Forks does exactly what I wanted to do.

-Aku

On Sat, Oct 3, 2009 at 8:54 PM, Elizabeth Mattijsen <li...@dijkmat.nl> wrote:
> On Oct 3, 2009, at 2:32 PM, Perrin Harkins wrote:
>>
>> On Fri, Oct 2, 2009 at 9:34 AM, Aku Kauste <ak...@gmail.com> wrote:
>>>
>>> Well, obviously this doesn't work and I end up getting "child pid
>>> XXXXX exit signal Segmentation fault (11)" type errors.
>>>
>>> Is my aproach completely wrong? What would be the right way to solve
>>> this kind of a problem?
>>
>> I believe what you're doing should work, but I don't use threads for
>> anything.  I'd probably fork and put the results in a database table
>> or file.
>
> If you like the threads API, you might want to take a look at the "forks"
> module on CPAN.  It provides the threads API using fork(), and hence does
> not have many of the problems that native Perl ithreads have.
>
>
> Liz
>

Re: Threading inside a handler script

Posted by Elizabeth Mattijsen <li...@dijkmat.nl>.
On Oct 3, 2009, at 2:32 PM, Perrin Harkins wrote:
> On Fri, Oct 2, 2009 at 9:34 AM, Aku Kauste <ak...@gmail.com>  
> wrote:
>> Well, obviously this doesn't work and I end up getting "child pid
>> XXXXX exit signal Segmentation fault (11)" type errors.
>>
>> Is my aproach completely wrong? What would be the right way to solve
>> this kind of a problem?
>
> I believe what you're doing should work, but I don't use threads for
> anything.  I'd probably fork and put the results in a database table
> or file.

If you like the threads API, you might want to take a look at the  
"forks" module on CPAN.  It provides the threads API using fork(), and  
hence does not have many of the problems that native Perl ithreads have.


Liz

Re: Threading inside a handler script

Posted by Perrin Harkins <ph...@gmail.com>.
On Fri, Oct 2, 2009 at 9:34 AM, Aku Kauste <ak...@gmail.com> wrote:
> Well, obviously this doesn't work and I end up getting "child pid
> XXXXX exit signal Segmentation fault (11)" type errors.
>
> Is my aproach completely wrong? What would be the right way to solve
> this kind of a problem?

I believe what you're doing should work, but I don't use threads for
anything.  I'd probably fork and put the results in a database table
or file.

- Perrin