Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Avoiding fork

by Anonymous Monk
on May 01, 2003 at 18:54 UTC ( [id://254765]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to make several system calls from a Tk GUI, and wanted to know if its possible to somehow background the process or am I forced into using fork?

In other words I need to make a system call tied to a button on the GUI, and then make several more system calls from that same button.

Replies are listed 'Best First'.
Re: Avoiding fork
by Zaxo (Archbishop) on May 02, 2003 at 01:38 UTC

    It's all fork under the hood.

    The choice depends on what you want. If you need output back from the process, use backticks or qx(). If the call is for its effects on the system, system. Both those put your program to sleep until the child exits.

    If you need fancier I/O with the process, call magic open, or one of the Open2, Open3 modules. Use fork when what you want requires fancier setup or logic than the others can provide.

    After Compline,
    Zaxo

Re: Avoiding fork
by defyance (Curate) on May 01, 2003 at 19:37 UTC
    You could use Proc::Daemon, to background(daemonize) a process. From what it sounds like your doing, however, I'd go with fork.

    -- Can't never could do anything, so give me and inch, I'll make it a mile.

Re: Avoiding fork
by BrowserUk (Patriarch) on May 02, 2003 at 06:09 UTC

    If you have a threaded perl, then you could start a thread at the start of your program and have it sit in a $q_cmds->dequeue() (Thread::Queue)loop. The button then uses $q_cmds->enqueue() to pass commands and parameters (or simply a "DO IT NOW" flag if the call is always the same) to the background thread and have it perform the backticks, system or IPC::Open2/3 as appropriate.

    The main thread would then call $q_results->pending(); every now and again to check if the results are ready and $q_results->dequeue() to fetch and display them. That would allow the gui to remain responsive while the system commands are processed, which I assume is your aim.

    Note: You need to create two queues before the thread is created, one foreground->background for the commands and one background->foreground for the results.

    If the program your are calling has an intereactive mode, then you might be able to avoid the overhead of re-loading the program each time by using something like Expect or Expect::Simple in the background thread.

    If the programs you are callling vary, you might be able to drive your shell this way, but the advantage diminishes as you would still be reloading the program itself each time.

    Whether any of the effort is worth it will depend on how many and how long running the command you are running are, and whether there is anything that the foreground thread can logically be doing whilst the background thread is running.


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-23 22:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found