Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: POE and Win32::Process control

by BrowserUk (Patriarch)
on Feb 13, 2005 at 21:22 UTC ( [id://430637]=note: print w/replies, xml ) Need Help??


in reply to Re^2: POE and Win32::Process control
in thread POE and Win32::Process control

That's a complex entity, rather beyond the scope of a "quick example". There are also some ambiguities in your description.

If processing is already done, they simply receive the output. If processing is not done, yet, they see the same (following) output of the process.

What if a read request is received for a process that has already started and produced half it's output? Does the new listener get everything that has already been sent to the file and any other listeners, or just that output the process produces after the request is received?

Anyway, here's how to use a thread to spawn a command and 'tee' it's output to a file, and retain a copy in memmory for your main thread to process at it convenience whilst also getting on with anything else it needs to do (like communicating with clients and monitoring queues):

#! perl -slw use strict; use threads qw[ yield async ]; use threads::shared; my( $cmd, $file ) = @ARGV; my $done : shared = 0; my @lines : shared; async { my $pid = open my $CMD, "$cmd |" or die "$cmd : $!"; open my $fh, '>', $file or die "$file : $!"; while( <$CMD> ) { chomp; print $fh $_; ## output to the file push @lines, $_; ## and push it to a shared array } $done = 1; }->detach; my $n = 0; while( !$done ) { if( @lines ) { ## lines to be processed print pop @lines; ## process them } else { ## Else nothing to do but wait. yield; } }

Whether that would work in conjunction with POE I'm not sure.

Personally, I would write the whole thing using threads, but that's rather more than your OP asked for, and would take a fair amount of effort.


Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

Replies are listed 'Best First'.
Re^4: POE and Win32::Process control
by m-rau (Scribe) on Feb 13, 2005 at 21:41 UTC
    > That's a complex entity

    I agree

    > some ambiguities in your description. (...) > What if a read request is received for > a process that has already started and > produced half it's output?

    Of cause, the user/ node who attached to a process which has not terminated, yet, after the process has already produced some output, the user first received the old output and next recieves the on-going output as it is produced by the continuing process.

    Thank you very much for your Thread example. I am excited about its cooperation with POE on win32.

      Two clarifications just in case:

      Thank you very much for your Thread example
    • Please note: The example uses threads not Thread!

      They are different and incompatible modules. The former requires 5.8.x, preferably 5.8.4 or later.

      The latter was for versions earlier that 5.8.0, is deprecated, and will cease to function in 5.10.x.

      I am excited about its cooperation with POE on win32.

      Note: I said that I am not sure whether threads and POE will work together, they may not. I am not sure if anyone has tried this combination.


      Examine what is said, not who speaks.
      Silence betokens consent.
      Love the truth but pardon error.
        I tried POE and threads. Even though they say something about it (http://poe.perl.org/?Poe_faq/is_poe_thread_safe) I have the impression, that POE is not thread-safe. win32 complains with an Attempt to free unreferenced scalar: SV 0x2afba80 during global destruction when I create a thread inside a session. The process continues. If it comes to a point, where this execution has to be triggered again, Perl aborts with an Application Error dialog - The instruction at bla bla referenced memory at bla bla. The memory could not be "read"..

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-25 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found