0

I want to run some code, like calling a huge database or network activity that will take time.

So how do I do it, in background and also to stop browser from waiting for background process to end without killing any process or breaking any flow

#!/usr/bin/perl

sub async_process{..what code..}
&async_process;

$T=time;
  print "Content-type: text/html; charset=utf-8\n\n";
  print "$T";
exit;
Atul Gupta
  • 725
  • 1
  • 6
  • 20

1 Answers1

1
sub async_process{
   my $pid = fork(); return if $pid; # creates new child process, and parent moves on
   close STDIN;close STDOUT; # releases browser from waiting child to finish
       # code goes here
       sleep 10;
       $T=time;open(_fh,">file.txt");print _fh "$T";close(_fh);
   exit;
}
Atul Gupta
  • 725
  • 1
  • 6
  • 20
  • check http://stackoverflow.com/questions/15842007/how-can-i-kill-forked-processes-that-take-too-long-in-my-perl-script-without-tim/15842451#15842451 on how to kill a long running child process – Atul Gupta Dec 01 '13 at 20:38
  • check http://docs.oracle.com/cd/E19205-01/819-5257/blaik/index.html on handling errors while creating fork – Atul Gupta Dec 01 '13 at 20:39