Allowing threading with CGI

A

alpha_beta_release

Hi,

I'm doing CGI, and plan to use threads.pm for CGI application. I ran a
test, but unfortunately, my http server (Apache) won't allow multiple
threads ran within a CGI script. But the script works OK if not as CGI
and as local running script. Any suggestion? Is this normal or just my
fault somewhere?

p/s This not to run multiple users simultenously (as using mod_perl or
something) but allow a user to run multiple threads (optimization)

thanks.
 
G

Gunnar Hjalmarsson

alpha_beta_release said:
I'm doing CGI, and plan to use threads.pm for CGI application. I ran a
test, but unfortunately, my http server (Apache) won't allow multiple
threads ran within a CGI script. But the script works OK if not as CGI
and as local running script. Any suggestion? Is this normal or just my
fault somewhere?

p/s This not to run multiple users simultenously (as using mod_perl or
something) but allow a user to run multiple threads (optimization)

Depending on which platform you are on, fork() may be a better solution.
The Parallel::ForkManager module facilitates the use of multiple processes.
 
A

alpha_beta_release

It doesn't work either. I modify this code from the example given.

Code:
------------------------------
#!c:/perl/bin/perl -Tw

use strict;
use Parallel::ForkManager;
use CGI;
use CGI::Carp 'fatalsToBrowser';

my $max_procs = 5;
my @names = qw( Fred Jim Lily Steve Jessica Bob Dave Christine Rico
Sara );

my $pm =  new Parallel::ForkManager($max_procs);
$pm->run_on_finish(
sub {
my ($pid, $exit_code, $ident) = @_;
}
);

$pm->run_on_start(
sub {
my ($pid,$ident)=@_;
}
);

$pm->run_on_wait(
sub {},
0.5
);

foreach my $child ( 0 .. $#names ) {
my $pid = $pm->start($names[$child]) and next;
# $pm->finish($child);
}

my $page = new CGI;
print $page->header,
$page->start_html,
'xxxxxx',
$page->end_html;
----------------------------

None of error messages, except system error :(

Is this (might be) because the system won't allow anonymous to run
threads and eat system resources?

p/s : Anyway i'm still try to make it work using this module. thanks
for the reference.
 
J

J. Gleixner

alpha_beta_release said:
It doesn't work either. I modify this code from the example given.

Code:
------------------------------
#!c:/perl/bin/perl -Tw

use strict;
use Parallel::ForkManager;
use CGI;
use CGI::Carp 'fatalsToBrowser';

my $max_procs = 5;
my @names = qw( Fred Jim Lily Steve Jessica Bob Dave Christine Rico
Sara );

my $pm =  new Parallel::ForkManager($max_procs);
$pm->run_on_finish(
sub {
my ($pid, $exit_code, $ident) = @_;
}
);

$pm->run_on_start(
sub {
my ($pid,$ident)=@_;
}
);

$pm->run_on_wait(
sub {},
0.5
);

foreach my $child ( 0 .. $#names ) {
my $pid = $pm->start($names[$child]) and next;
# $pm->finish($child);
}

my $page = new CGI;
print $page->header,
$page->start_html,
'xxxxxx',
$page->end_html;
----------------------------

None of error messages, except system error :(

That's not very helpful. What system error?
Is this (might be) because the system won't allow anonymous to run
threads and eat system resources?

No. Before jumping to Parallel::ForkManager, read up on how CGI works.

use CGI;
use Parallel::ForkManager;

my $page = new CGI;
print $page->header,
$page->start_html;

my $max_procs = 5;
my @names = qw( Fred Jim Lily Steve Jessica Bob Dave Christine Rico Sara );

my $pm = new Parallel::ForkManager($max_procs);

for my $child ( 0 .. $#names ) {
my $pid = $pm->start($names[$child]) and next;
print "Running $names[$child] pid=$pid<br> ";
sleep 1;
$pm->finish($child);
}

$pm->wait_all_children();

print $page->end_html();
 
X

xhoster

alpha_beta_release said:
Hi,

I'm doing CGI, and plan to use threads.pm for CGI application. I ran a
test, but unfortunately, my http server (Apache) won't allow multiple
threads ran within a CGI script. But the script works OK if not as CGI
and as local running script. Any suggestion?

Yeah, tells what the problem is. Did you get the notorious
"Neener-neener, your mom wears combat boots at line 42" error?

Xho
 
A

alpha_beta_release

That's the problem. Nothing 'informative' error. It just says,
"application.blablabla encountered problem. Please send blablabla"
(windows error, duh!). I try to run it locally (localhost). When i run
on Linux, nothing shows up on browser (as server reply).

But if i try run as script (not CGI), it works. Wonder why... :?
 
X

xhoster

alpha_beta_release said:
It doesn't work either. I modify this code from the example given.

Code:
------------------------------

foreach my $child ( 0 .. $#names ) {
my $pid = $pm->start($names[$child]) and next;
# $pm->finish($child);
}[/QUOTE]

Don't comment out the $pm->finish.  It is there for a reason
[QUOTE]
----------------------------

None of error messages, except system error :(

Which system error?

Xho
 
X

xhoster

alpha_beta_release said:
That's the problem. Nothing 'informative' error.

Anything is more informative than nothing.
It just says,
"application.blablabla encountered problem. Please send blablabla"
(windows error, duh!).

Sounds like the windows equivalent of unix's seg-fault. Someone more
familiar with Windows might be able to divine the entrails of the blablabla
if you would tell us what they actually are.
I try to run it locally (localhost). When i run
on Linux, nothing shows up on browser (as server reply).

I have no problem using either fork of threads with CGI on linux.

Xho
 
A

alpha_beta_release

That's weird. Is it just my system?
I don't think there's any informative thing about the message. Anyway,
here's the message
"Perl Command Line Interpreter has encountered a problem and needs to
close. We are sorry for the inconvenience."

I run it on Windows, using Apache as HTTP server

Here's the code
Code:
------------------------------------------------------------
#!c:/perl/bin/perl -Tw

use strict;
use Parallel::ForkManager;
use CGI;
use CGI::Carp 'fatalsToBrowser';       # debug

my $max_procs = 5;
my @names = qw( Fred Jim Lily Steve Jessica Bob Dave Christine Rico
Sara );

my $pm =  new Parallel::ForkManager($max_procs);
$pm->run_on_finish(
sub {
my ($pid, $exit_code, $ident) = @_;
}
);

$pm->run_on_start(
sub {
my ($pid,$ident)=@_;
}
);

$pm->run_on_wait(
sub {},
0.5
);

foreach my $child ( 0 .. $#names ) {
my $pid = $pm->start($names[$child]) and next;
$pm->finish($child);
}

my $page = new CGI;
print $page->header,
$page->start_html,
'xxxxxx',
$page->end_html;
------------------------------------------------------------
 
X

xhoster

alpha_beta_release said:
That's weird. Is it just my system?
I don't think there's any informative thing about the message. Anyway,
here's the message
"Perl Command Line Interpreter has encountered a problem and needs to
close. We are sorry for the inconvenience."

I run it on Windows, using Apache as HTTP server

It worked for me on Linux with Apache. Sorry, I can't test it on
Windows/Apache. Now that we have working code, maybe someone else can.

Maybe it is an Apache for Windows configuration issue.

Xho
 
A

alpha_beta_release

And here excerpt from the error.log

[Tue Sep 05 12:02:36 2006] [notice] Apache/2.2.2 (Win32) configured --
resuming normal operations
[Tue Sep 05 12:02:36 2006] [notice] Server built: Apr 29 2006 18:32:31
[Tue Sep 05 12:02:36 2006] [notice] Parent: Created child process 4004
[Tue Sep 05 12:02:36 2006] [notice] Child 4004: Child process is
running
[Tue Sep 05 12:02:36 2006] [notice] Child 4004: Acquired the start
mutex.
[Tue Sep 05 12:02:36 2006] [notice] Child 4004: Starting 250 worker
threads.
[Tue Sep 05 12:02:36 2006] [notice] Child 4004: Starting thread to
listen on port 80.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top