[Last Call] Learn how to a build a cloud-first strategyRegister Now


trying to get an async perl function to run in my CGI

Posted on 2007-08-01
Medium Priority
Last Modified: 2013-12-25
hello i am trying to get an async perl function to run in my CGI
so here is my little snippet
it seems that i do not get the After thread print until search returns;
thanks for any help
print "starting thread <BR>";
$t = new Thread(search);
use Thread 'async';
print "after thread: the finished results will be in <BR>";
Question by:paries
LVL 25

Expert Comment

ID: 19615170
Autoflushing your output may get the "after thread" line to show up sooner, but pretty sure the CGI process will continue to hang around (and hold open your web server / browser connection) until all the threads exit-- maybe that's what you're after, don't really know with the little bit of explanation and code you posted.

$| = 1;  # autoflush
print "starting thread <BR>";
$t = new Thread(search);
use Thread 'async';
print "after thread: the finished results will be in <BR>";

Author Comment

ID: 19615926
actually i want the search $t = new Thread(search);
 to go off on its own(it has no browser interaction) an the browser to continue and finish.

looks ike i may have to make the cgi call an exterbal system call instead?//
LVL 39

Accepted Solution

Adam314 earned 2000 total points
ID: 19617382
I think you'll need to do a fork (to create a new process, not a new thread), then detach the new process from the main process.

Something like this:

use POSIX qw(setsid);

defined(my $pid = fork)   or die "Can't fork: $!";
if($pid) {
    #parent stuff goes here
else {
    #child needs to become detached.  It should also redirect its STDIN, STDOUT and STDERR,
    #as you don't want this showing up anywhere...
    chdir '/'                 or die "Can't chdir to /: $!";
    open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
    open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
    open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
    setsid                    or die "Can't start a new session: $!";
    umask 0;

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question