Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Second background process is pausing the gui

by bliako (Monsignor)
on Jun 11, 2020 at 10:59 UTC ( [id://11117937]=note: print w/replies, xml ) Need Help??


in reply to Re: Second background process is pausing the gui
in thread Second background process is pausing the gui

It will be asynchronous in the sense that the command output can be read while it is being executed (as a child process). Otherwise it is synchronous in the sense that you can't move outside the scope of the filehandle $xx without explicitly killing the child.

This is the normal usage based on "Perl Cookbook"'s "16.4. Reading or Writing to Another Program" (edit: sorry for pirated link! I did not know, thanks Fletch)

my $pid = open(my $xx, "od -w1 -v -An -N5 -td1 < /dev/random 2>&1|") or die "fork: $!"; if( $pid ){ while(<$xx>){ print "read: $_"; } } close($xx) or die "command failed with ".($?>>8);

The following code demonstrates what I mean by "outside the scope of the filehandle". At the goto it waits for the spawned command to be finished:

{ my $pid = open(my $xx, "sleep 10|") or die "fork: $!"; if( $pid ){ goto XX; while(<$xx>){ print "read: $_"; } } close($xx) or die "command failed with ".($?>>8); } XX: print "here\n";

You will notice that "here" is printed only upon script exit. Whereas, the following executes the goto because it is within scope but script does not exit until sleep exits. Perhaps half-asynchronously would be a better term ;)

my $pid = open(my $xx, "sleep 10|") or die "fork: $!"; if( $pid ){ goto XX; while(<$xx>){ print "read: $_"; } } close($xx) or die "command failed with ".($?>>8); XX: print "here\n";

"here" is printed at the goto, then there is implicit waiting for sleep to end.

bw, bliako

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11117937]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-03-29 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found