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
Aku Kauste [ Fr, 02 Oktober 2009 15:34 ] [ ID #2017798 ]

Re: Threading inside a handler script

On Fri, Oct 2, 2009 at 9:34 AM, Aku Kauste <aku.kauste [at] 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
Perrin Harkins [ Sa, 03 Oktober 2009 14:32 ] [ ID #2017892 ]

Re: Threading inside a handler script

On Oct 3, 2009, at 2:32 PM, Perrin Harkins wrote:
> On Fri, Oct 2, 2009 at 9:34 AM, Aku Kauste <aku.kauste [at] 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
Elizabeth Mattijsen [ Sa, 03 Oktober 2009 19:54 ] [ ID #2017894 ]

Re: Threading inside a handler script

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

=2D-
Need professional mod_perl support?
Just hire me: torsten.foertsch [at] gmx.net
torsten.foertsch [ Sa, 03 Oktober 2009 21:00 ] [ ID #2017895 ]
Webserver » gmane.comp.apache.mod-perl » Threading inside a handler script

Vorheriges Thema: ModPerl::Registry and Error Documents
Nächstes Thema: [MP2][SESSION::POSTGRES] Problems retrieving session data.