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

When on Windows, I do:
use Win32; use Win32::Process; Win32::Process::Create($pobj, 'D:\apps\Ghostgum\gsview\gsview32.exe', 'D:\apps\Ghostgum\gsview\gsview32.exe' . "fn.ps", 0, NORMAL_PRIORITY_CLASS, ".") || die 'can"t Win32::Process::Create: gsview32.exe'. Win32::FormatM +essage(Win32::GetLastError());
Win32::Process::Create is needed to spawn process in the background, so my GUI do not freezes.

How do I do this on Linux?

TIA!
Vadim.

Replies are listed 'Best First'.
Re: How to spawn a process in background within Perl+GUI script
by saintmike (Vicar) on Apr 13, 2006 at 14:32 UTC
    When you're spawning a process within a program running a GUI, there's a few gotchas. You need to make sure the GUI keeps ticking and you probably need to communicate between the GUI and your extra process.

    An elegant way of handling this is POE. It lets you hook into various GUI event loops (Gtk, perl/Tk, etc.). Check out this article on using POE with Gtk.

    Another way of communicating between the GUI and the spawned process are watchers in the GUI, here's an article which uses an XML-defined GUI and Glib's add_watch method.

      The article is outstanding. What I would like to do is take the code discussed in the article and adjust it to access Yahoo's Stock Screener. The Stock Screener lets you filter and grab stock prices throughout the day by a bunch of different criteria. One can, for example, use it to download a CSV file of the latest price of every single stock on a particular exchange, such as the NYSE.

      If anyone has code that automates the interface with Yahoo Stock Screener, I'd love to see it. Otherwise, I'll put something together and post it.
      I needed something much more simplistic, because this time I did not need control spawned program.

      Thanks for excellent articles, BTW!

Re: How to spawn a process in background within Perl+GUI script
by salva (Canon) on Apr 13, 2006 at 13:57 UTC
Re: How to spawn a process in background within Perl+GUI script
by Anonymous Monk on Apr 13, 2006 at 13:36 UTC
    system "kghostview fn.ps &";

    See system.

      I suspected I overlooked something that simple...
      Works perfectly, thanks!
Re: How to spawn a process in background within Perl+GUI script
by zentara (Cardinal) on Apr 13, 2006 at 17:24 UTC
    How do I do this on Linux?

    It depends on what GUI toolkit you want to use, but in general there are 3 ways.

    The major consideration, is 'not blocking the gui', otherwise known as 'keeping the event loop going'. In gui's, the event-loop is the main program which is running, so if you need to run another process, it's either "fork-and-exec", threads, or finding a way to constantly call "update" or "do-one-event-loop" .

    You really need to be more specific in your question, because there are MANY different ways to do it, and it all depends on the GUI toolkit, and the program you need to spawn( and whether you need realtime results from the spawned program).


    I'm not really a human, but I play one on earth. flash japh
Re: How to spawn a process in background within Perl+GUI script
by chibiryuu (Beadle) on Apr 13, 2006 at 20:35 UTC
    This should suit your needs as far as I can tell, from the little that you've written.
    defined ($pid = fork) or die "couldn't fork: $!"; $pid or exec 'gsview', ...
    wait $pid; # == $pobj->Wait kill TSTP => $pid; # == $pobj->Suspend kill CONT => $pid; # == $pobj->Resume