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

Posted on 2007-08-01
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

    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

    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

    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;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    The viewer will learn how to dynamically set the form action using jQuery.
    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.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now