in reply to Re^2: Using Proc::Background and Win32
in thread Using Proc::Background and Win32

Hey BrowserUK, thanks again for the reply!

Humm.... I was going to do your example but your first line confused me a bit... Sorry, I'm not brand new to
Perl or anything like that but I'm definitely no expert.

But since my script uses Gtk2, I can't really use the "Sleep" command in the final 'production' script, since
the regular 'sleep' command will pause the processing of Gtk events.

But for pure testing purposes only I tried this and it seemed to work:
my $pid = system 1, ".\\bg_script.pl $ARG1 \"$ARG2\" $ARG3 $ARG4"; print "\$pid == '$pid'\n"; while (kill 0, $pid) { print kill 0, $pid; print "\n"; sleep 1; } print "\n\nOUT OF WHILE LOOP\n\n"; ______________OUTPUT______________ C:\Script_dir>main_script.pl $pid == '3144' 1 1 1 1 1 1 0 OUT OF WHILE LOOP

Nice, so it looks like its working that way.... Weird, do you think I had a typo the first time I posted
that, or do you think I can't set it to a variable and check it that way?

And just so you know, in my final script that was working before, with Proc::Background, which was before
I attempted using the CavaPackager to turn this into a standalone executable. I was using a while loop and the
usleep command from --> "use Time::HiRes qw( usleep );" which sleeps so quickly that I was able to continue
processing Gtk commands during each loop. I can show the code for that if your interested...


But anyway, in terms of the Win32::Process, when reading the cpan page for Proc::Background, it sounded like
that Proc::Background Module when run on Win32 it automatically uses the Win32::Process Module.
Is that right, or to try that Win32::Process, do I need to specifically call that, because it looks
like there is quite a good bit of a difference between the two?


Thanks Again,
Matt

Replies are listed 'Best First'.
Re^4: Using Proc::Background and Win32
by BrowserUk (Patriarch) on Oct 18, 2013 at 21:36 UTC
    But since my script uses Gtk2, I can't really use the "Sleep" command in the final 'production' script, since the regular 'sleep' command will pause the processing of Gtk events.

    I've never used gtk2, but it presumably has repeating timers like Tk. So, set up a repeating timer that does the kill.

    Nice, so it looks like its working that way.... Weird, do you think I had a typo the first time I posted that, or do you think I can't set it to a variable and check it that way?

    Dunno about a typo; but I strongly recommend you use 'perl' as the second arg to system.:

    my $pid = system 1, 'perl', ".\\bg_script.pl $ARG1 \"$ARG2\" $ARG3 $AR +G4";

    Without that, perl starts a shell, and the shell invokes perl; but the pid you get back is that of the shell not perl, so it doesn't tell you when the perl process ends.

    I attempted using the CavaPackager t

    Sorry. I have no knowledge of that at all.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      CavaPackager is a program that has a GUI you can use to 'build' perl scripts into standalone executables.
      I plan on putting this program on a few different PCs around the office and don't want to have to install
      Perl, as well as ALL the required Modules, espically Gtk2/Pango/Cairo/etc on each and every PC I want this on...
      Which is rather time consuming.


      Here is my ORIGINAL Code Snippet which executes the Background Script:
              *This is only a small snippet to get the idea...
              *FYI, the Background script opens a TCP Socket and sends some data to a server.

      my $TIMEOUT = 30; my $proc; my $bg_script = "./bg_script.pl"; $proc = Proc::Background->new("perl $bg_script $ARG1 \"$ARG2\" $ARG3 $ +ARG4"); my $PID = $proc->pid; my $start_time = $proc->start_time; my $alive = $proc->alive; my $timeout_start = time; my $timeout_current; my $timeout_diff; while($alive ne 0) { $alive = $proc->alive; # This will continue the processing of Gtk Events... while (Gtk2->events_pending) { Gtk2->main_iteration; } Gtk2::Gdk->flush; $timeout_current = time; $timeout_diff = $timeout_current - $timeout_start; if ($timeout_diff >= $TIMEOUT) { last; #->Use 'last' to break out of loop if we +timed-out... } # Sleep for 1000 milliseconds usleep(1000); }

      I'm not exactly sure how it works, this is my first time using the CavaPackager Program, but after it's
      done it will be a ".exe" and not a ".pl" so not sure if leaving the "perl" before the script will work in
      the end...


      But back to the Win32::Process, does anyone think this would be better to use then using the system command
      that I said I got working in my last post?

      I was just kinda curious if there were any Pros and/or Cons between using that system call method I got
      working, and the "Win32::Process" method (*I guess only if it differs from Proc::Background)...

      But anyway, I'm heading home shortly, I think I just need to step away from this for a bit and take a breath.
      My head is sort of spinning with all this stuff.


      Thanks AGAIN to everyone who posted, much appreciated!!


      Thanks,
      Matt

        But back to the Win32::Process, does anyone think this would be better to use then using the system command

        The bottom line is that all of them -- including system which ends up calling win32_spawnvp() in Win32.c -- use the OS call CreateProcess().

        What distinguishes them, is the preprocessing of the arguments and command strings, prior to calling CreateProcess(), and in that respect, system does it better than all of the others.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.