Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Can I run a piece of code like child process?

by Anonymous Monk
on Oct 03, 2000 at 21:18 UTC ( #35146=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on Oct 03, 2000 at 21:18 UTC
Q&A  > programs and processes


HI,I want to run a piece of code (i.e A subroutine) in my script and the main code will not wait for his done.
The main code should go on and will not be care of whether the subroutine run correctly.
I don't want to use fork(),because child process occupy too many memory.Is there a way?
Thank you!

Answer: Can I run a piece of code like child process?
contributed by tilly

Use fork anyways. It is more efficient than you think.

See Threads vs Forking (Java vs Perl) and subsequent discussion (particularly the article that KM pointed out).

And don't use threads if you care about reliability, stability, and all that. Perl's threading does...interesting things.

Answer: Can I run a piece of code like child process?
contributed by OzzyOsbourne

I honestly have not checked the overhead on win32::process, and most people seem to be against it but I use:

use Win32::Process; @servers=('server1','server2','server3'); sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); } foreach $server (@servers){ Win32::Process::Create($ProcessObj, "c:\\perl\\bin\\perl.exe", "perl.exe c:\\ -s$server", 0, NORMAL_PRIORITY_CLASS, ".")|| die ErrorReport(); #$ProcessObj->Suspend(); #$ProcessObj->Resume(); #$ProcessObj->Wait(INFINITE); }
With the wait remmed out, the sub will not wait for the process.
Answer: Can I run a piece of code like child process?
contributed by Eradicatore

From what I've experienced and researched so far on win32 and NT is that its easiest maybe to use fork. From what I can tell, fork on win32 is like a hybrid between a true fork and a thread. It doesn't create a new seperate process as seen by the task manager, but it also does not have access to global variables in the parent like a thread should.

My example shows how to use fork together with Tk to get web pages but not interfere with the gui. The child just sits there waiting for urls to get. You will have to add proxy support if you're behind a firewall though.

Oh, also, you want to make sure you download the right version of perl that supports fork. You can get this from active state at here

#!/usr/local/bin/perl use POSIX; use Tk; use LWP::UserAgent; use URI::URL; pipe(FROM_P, TO_C) or die "pipe: $!"; select(((select(TO_C), $| = 1))[0]); my $ua = LWP::UserAgent->new; if (!($pid = fork)) { close(TO_C); while($line = <FROM_P>) { print "Child Pid $$ just read this: $line\n"; chomp($line); $url = $line; $file = 'yahoo.txt'; my $req = HTTP::Request->new(GET => $url); $req->header('Accept' => 'text/html'); $res = $ua->request($req, $file); }; } my $mw = MainWindow->new(); $button = $mw->Button(-text => "click here to download yahoo web page" +, -activebackground => 'red', -command => \&printit)->pack(); print "i'm the parent about to mainloop\n"; MainLoop; sub printit { print TO_C "http:\/\/www\.yahoo\.com\n"; }

Justin Eltoft

"If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

Answer: Can I run a piece of code like child process?
contributed by AgentM

use Threads; perlthrtut

Answer: Can I run a piece of code like child process?
contributed by harpreet

Thanks a lot guys for helping me out...even I was looking out for a way to run a child process without the parent process having to wait....thanks for helping Harpreet

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (4)
    As of 2020-10-25 22:34 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (249 votes). Check out past polls.