Threading inside a handler script

Threading inside a handler script

am 02.10.2009 15:34:17 von Aku Kauste

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 "

Got: $ret

";

__END__

- Aku

Re: Threading inside a handler script

am 03.10.2009 14:32:34 von Perrin Harkins

On Fri, Oct 2, 2009 at 9:34 AM, Aku Kauste 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

Re: Threading inside a handler script

am 03.10.2009 19:54:43 von Elizabeth Mattijsen

On Oct 3, 2009, at 2:32 PM, Perrin Harkins wrote:
> On Fri, Oct 2, 2009 at 9:34 AM, Aku Kauste
> 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

am 03.10.2009 21:00:44 von torsten.foertsch

On Sat 03 Oct 2009, Elizabeth Mattijsen wrote:
> > I believe what you're doing should work, but I don't use threads
> > for anything. =A0I'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 =A0
> "forks" module on CPAN. =A0It provides the threads API using fork(),
> and =A0 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=20
does not use kernel threads but implements perl stack switches. So, you=20
have to watch out to avoid blocking operations. Similar to "win 3.x=20
multitasking" or early UNIX threading approaches.

Torsten

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