Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Second background process is pausing the gui by bliako
in thread Second background process is pausing the gui by Ohad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-24 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found