Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Second background process is pausing the gui

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


in reply to Second background process is pausing the gui

Is $reg_out global? If yes, could fileevent be cancelling the current bind and binding to new shell-out? Similar problem of "freezing" was solved here: Perl::TK - fileevent and script execution theory

BTW, you certainly don't want exec because it replaces the current perl process and once finished, you program will exit.

Outside the Tk realm, if you want to run a background process you could fork first and then spawn/shellout. Like:

my $pid = fork() // die "fork: $!"; if( $pid ){ print "forked: i am shelling out\n"; spawn(); print "after shelling out.\n"; } print "I am now waiting for background process.\n"; waitpid $pid, 0; print "done.\n"; sub spawn { my $pid = open(my $xx, "sleep 1 2>&1|") or die "fork: $!"; print "implicitly waiting for child...\n"; # close($xx); }

Also, I don't know what side-effects fileevent has on the above code, but usually when forking out to an external command you must wait for it to finish. Essentially, close($reg_out) acts as a waitpid $status, 0;. But if you don't, then perl will implicitly wait for it. If the code you posted is a sub, it will not exit until $cmd is dead (again, I don't know how fileevent interferes with this if at all).

So, something like this:

print "before spawn\n"; spawn(); print "after spawn\n"; sub spawn { my $pid = open(my $xx, "sleep 3 2>&1|") or die "fork: $!"; print "implicitly waiting for child...\n"; # the proper way to wait for the command to exit: # close($xx); } # output: before spawn implicitly waiting for child... # sleep... after spawn

UPDATE: exec on the other hand is used in a trick where the program forks using open(my $xx, '|-'); and at the child branch the external command is spawned using exec (which offers finer control over open if one wants to avoid the shell). But that's different to calling exec() in your normal program flow (as an alternative to system()) as it will kill your program.

bw, bliako

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found