Jump to content

Listening To Multiple Child Process


 Share

Recommended Posts

I have a Parent Script that starts multiple, concurrent child processes. I cannot figure out how to apply the technique for using StdoutRead in the examples in a way that listens to them all simultaneously.

For reference, there can be any even number of, or single, child processes and there is an 1D array, $PIDS, that stores the PIDS of all the child processes and they should be outputting to another 1D array $sOutput.

Thank you for your time. Please just point me in the right direction. I'm sorry if I missed another topic that covers this.

Edited by Funtime60
Edit 2: Forgot to add a reason for Edit 1: Grammar
Link to comment
Share on other sites

Hi,

How to Ask Help

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

Could you please be more complete in what you think I missed? I cannot think of anything other than what I tried, which I will add when my laptop can reach the site. I'd like to help anyone who wants to try and assist me with my problem. Thank you.

Edited by Funtime60
Edit 1: wanted to mention that this is from my phone
Link to comment
Share on other sites

Both StdoutRead and StdinWrite require the PID of the child as the first parameter.  So you just have to poll (read) each of the PIDs to see if the childs have sent you something.  Since StdoutRead is a non-blocking function, there is no issue opting for this strategy.  If you need more detailed help, I would suggest you post a small snippet of both your parent and child in order for us to better understand your problem.

Link to comment
Share on other sites

I realize that I should have posted code now, which I will do now that it seems my school has unblocked autoitscript.com on the WiFi. Child must be compiled in same directory currently. It must also be named Child1TMP.exe currently. My problem seems to be that my parent keeps trying to pull data even after the children have stopped outputting.

;Snippet of the Parent
Local $x = 100
Local $y = 100
Local $x2 = 100 + $x
Local $y2 = 100 + $y
Local $ChildCount = 4
Local $PIDS[$ChildCount]
Local $PIDSMask = $PIDS
Local $PCount = 0
For $ix = 0 To (2)-1
    For $iy = 0 To (2)-1
        $PIDS[$PCount] = Example($x*$PCount, $y, $x2, $y2)
        $PCount += 1
    Next
Next
For $MCount = 0 To UBound($PIDSMask)-1
    $PIDSMask[$MCount] = 0
Next

Local $sOutput[$ChildCount]
While 1
    For $RCheck = 0 To UBound($PIDS)-1
        If $PIDSMask[$RCheck] =0 Then
            $sOutput[$RCheck] &= StdoutRead($PIDS[$RCheck])
            ConsoleWrite($RCheck&@TAB&StdoutRead($PIDS[$RCheck])&@CRLF) ;Debug
        EndIf
        If @error Then
            $PIDSMask[$RCheck] += 1
            ExitLoop
        EndIf
    Next
WEnd

Func Example($fsx,$fsy,$fdx,$fdy)
    Local $iPID = Run(@ScriptDir&"\Child1TMP.exe", @ScriptDir, @SW_HIDE, 3)

    StdinWrite($iPID, $fsx & @CRLF & $fsy & @CRLF & $fdx & @CRLF & $fdy & @CRLF)

    StdinWrite($iPID)

    Return($iPID)
EndFunc

;Snippet of the Child
Local $sOutput = ""
While True
    $sOutput &= ConsoleRead()
    If @error Then ExitLoop
    Sleep(25)
WEnd

TestWrite($sOutput)

Func TestWrite($input)
    Local $Stuff = StringSplit($input, @CRLF, 1)
    For $x = $Stuff[1] To $Stuff[3]+$Stuff[1]
        For $y = $Stuff[2] To $Stuff[4]+$Stuff[2]
            ConsoleWrite($x&","&$y&@CRLF) ;Output
        Next
    Next
EndFunc

Thank you

Link to comment
Share on other sites

Your @error should be after the StdOutRead, and not after the EndIf, it will never get an error message that way, the ConsoleWrite resets the error value before you try to read it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Like Brew said the @error is misplaced but I also think your dual loop is not working properly.  It should a 3 levels loops.  If you read help file carefully it says : 

StdoutRead() does not block, it will return immediately. In order to get all data, it must be called in a loop.

So in your snippet, you will exitloop before going thru all your childs.  I don't believe this is want you want.  I think you want to exitloop when you reach EOF.

Link to comment
Share on other sites

I've done something a bit dumb. I've asked you all for help, but now I've gone and changed how my code is intended to function. The only part I think that still applies is this:

4 hours ago, Nine said:

StdoutRead() does not block, it will return immediately. In order to get all data, it must be called in a loop.

I say this because even after looking at the help file, I still have no idea what it means by it "does not block". Am I correct in assuming that this means that the StdoutRead()  won't be able to pull anything after the process closes?

I'll post an updated snippet when I finish the changes.

Edited by Funtime60
Link to comment
Share on other sites

33 minutes ago, Funtime60 said:

Am I correct in assuming that this means that the StdoutRead()  won't be able to pull anything after the process closes?

No, it means that using the function, it  will not stop the script at that function.  It will continue returning a value that says something it has nothing to read.  Unlike for example a msgbox () which ofc is totally different, it will block the script till the user respond to the message...

Edit : for everyone who will read this.  When you look at the help file and you do not understand a sentence, you should know that you are missing something important.  All single words in this help file are truly essential. Read carefully multiple times the help subject, try the examples, modify the examples at the places you are unsure of the meaning.  This is the best recommandation I can give...

Edited by Nine
Link to comment
Share on other sites

Hi @Funtime60, at first glance on your listing I see 2 inaccuracies quite macroscopic .

  1. One is that you try to read twice the same data from the StdOut straem buffer. Once you read  the I/O stream from your process, that stream buffer is emptyed, so next read will get data only if new data is present, otherwyse you get an empty string (and not an @error). Of course the first stdoutread, stores the read data in the  $sOutput[$RCheck] variable, so you have to use that variable to 'see' and manage data read.
  2. two, as already pointed out by BrewManNH and Nine, the  @error macro is misplaced. In short the use of the @error macro to check if a process has finished, it should be placed right after the StdOutread statement, so if the @error is set to true it means that the Pid you are trying to read doesn't exists anymore, tht is, it has finished and closed himself. (you can still read data from the stdoutread buffer even if in some cases I got issues in that way). Another maybe more kind way to check if a process has finished is to use the ProcessExist() function.

Here your script a little modified (quick and dirty) should do what you expect (at least I hope...)
P.S. maybe you can also be interested to this link where there is a ready made way to run and manage many external process.

;Snippet of the Parent
Local $x = 100
Local $y = 100
Local $x2 = 100 + $x
Local $y2 = 100 + $y
Local $ChildCount = 4
Local $PIDS[$ChildCount]
Local $PIDSMask = $PIDS
Local $PCount = 0
For $ix = 0 To(2) - 1
    For $iy = 0 To(2) - 1
        $PIDS[$PCount] = Example($x * $PCount, $y, $x2, $y2)
        $PCount += 1
    Next
Next
For $MCount = 0 To UBound($PIDSMask) - 1
    $PIDSMask[$MCount] = 0
Next

Local $sOutput[$ChildCount], $TasksDone = 0
; While 1
Do
    $TasksDone = 0
    For $RCheck = 0 To UBound($PIDS) - 1

        ; If $PIDSMask[$RCheck] =0 Then
        $CheckData = StdoutRead($PIDS[$RCheck]) ; check presence of new data on the $PID's stream
        If $CheckData <> "" Then ; if we got something then keep it
            $sOutput[$RCheck] &= $CheckData ; stack task's stdout
            ; ConsoleWrite($RCheck&@TAB&StdoutRead($PIDS[$RCheck])&@CRLF) ; StdOut already read!!, you con't read twice same data
            ConsoleWrite($RCheck & @TAB & $sOutput[$RCheck] & @CRLF) ;Debug ; print already got data to console
        EndIf
        ; EndIf
        ; If @error Then
        If Not ProcessExists($PIDS[$RCheck]) Then
            $TasksDone += 1
            ; $PIDSMask[$RCheck] += 1
            ; ExitLoop
        EndIf
    Next
Until $TasksDone = UBound($PIDS)
; WEnd

Func Example($fsx, $fsy, $fdx, $fdy)
    Local $iPID = Run(@ScriptDir & "\Child1TMP.exe", @ScriptDir, @SW_HIDE, 3)

    StdinWrite($iPID, $fsx & @CRLF & $fsy & @CRLF & $fdx & @CRLF & $fdy & @CRLF)

    StdinWrite($iPID)

    Return($iPID)
EndFunc   ;==>Example

#cs
    ;Snippet of the Child
    Local $sOutput = ""
    While True
    $sOutput &= ConsoleRead()
    If @error Then ExitLoop
    Sleep(25)
    WEnd
    
    TestWrite($sOutput)
    
    Func TestWrite($input)
    Local $Stuff = StringSplit($input, @CRLF, 1)
    For $x = $Stuff[1] To $Stuff[3] + $Stuff[1]
    For $y = $Stuff[2] To $Stuff[4] + $Stuff[2]
    ConsoleWrite($x & "," & $y & @CRLF) ;Output
    Next
    Next
    EndFunc   ;==>TestWrite
#ce

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...