Jump to content

Communication between 2 scripts


Recommended Posts

Hi,

I would like to send an array from a script to a another. This array has 1000 rows and 4 columns with this kind of data :

1st row  =     528  ;  31  ;   HOSTNAME|1|02:45:47|abcdefgh|username|5   ;   old

2nd row = ...

What IPC should I use ?

 

 

Link to post
Share on other sites

I think it could be a situation of parent - child communication between 2 processes (compiled scripts).

with the use of consoleread / consolewrite and stdin / stdout

we will need more info to help you out

Edited by Nine
Link to post
Share on other sites

https://www.autoitscript.com/autoit3/docs/libfunctions/_FileWriteFromArray.htm

https://www.autoitscript.com/autoit3/docs/libfunctions/_FileReadToArray.htm

It's not perfect real time but should work in most cases.

 

Toss in some file handling and file cleanup to keep things working properly. 

 

Or go the extra mile and use a database.

Link to post
Share on other sites
34 minutes ago, ViciousXUSMC said:

It's not perfect real time but should work in most cases.

But if you need real time, you could use @ViciousXUSMC idea combine with a simple protocol using my approach of parent-child inter-process communication. 

Link to post
Share on other sites
Link to post
Share on other sites

Hi tatane :)


Here are 2 scripts showing how to pass a 2 Dimension Array from a script to another.
With the help of jguinch's ArrayMultiDim.au3, it's possible, here is the link of his work :

https://www.autoitscript.com/forum/topic/179779-_arraydeclarefromstring-_arraytodeclarationstring-_arrayshufflemultidim-_arraycompare-_arrayenumvalues-_arrayassign/

He's got a function that transposes the whole Array into a String, it's the String that will be passed as a Parameter from a script to the other. Then a 2nd function will transpose the String back to an Array, job done !

I attach at the end of this post jguinch's ArrayMultiDim.au3 but it's a good idea to read what he said about all the functions he created. After download, just place his file ArrayMultiDim.au3 in the same folder as the 2 scripts below, named "script part1.au3" and "script part2.au3"

; This is "script part1.au3"
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <ArrayMultiDim.au3> ; Author jguinch

Local $aArray[3][4]
For $row = 0 to 2
    For $col = 0 to 3
        $aArray[$row][$col] = $row & ":" & $col
    Next
Next

_ArrayDisplay($aArray, "in script 1 : Array", "", $ARRAYDISPLAY_COLALIGNCENTER)

Local $sStringFromArray = _ArrayToDeclarationString($aArray)
MsgBox($MB_TOPMOST, "in script 1 : String From Array", $sStringFromArray)

$iPID = Run(@AutoItExe & " /AutoIt3ExecuteScript " & chr(34) & @ScriptDir & "\script part2.au3" & chr(34) & _
    " " & $sStringFromArray)

If $iPID = 0 Then
   MsgBox($MB_TOPMOST, "Error", "'script part2' didn't lauch")
   Exit
Else
   ProcessWaitClose($iPID)
   MsgBox($MB_TOPMOST, "Back to script 1", "Great : this message shows only after 'script part2' has ended !")
EndIf

script1-a.jpg.67fb41b4a8f60479bc8af9919c540c10.jpgscript1-b.jpg.be621c538060999d62a516af284d41fe.jpg

; This is "script part2.au3"
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <ArrayMultiDim.au3> ; Author : jguinch

$sParams = "$CmdLineRaw = " & $CmdLineRaw & @CRLF
For $i = 0 To $CmdLine[0]
   $sParams &= @CRLF & "$CmdLine[" & $i & "] = " & $CmdLine[$i] & @CRLF
Next

MsgBox($MB_TOPMOST, "in script 2 : Parameters passed from 'script part1'  to  'script part2'", _
    Eval("sParams"))

MsgBox($MB_TOPMOST, "in script 2 : Right part of $CmdLineRaw", _
    StringMid($CmdLineRaw, StringInStr($CmdLineRaw, "[[")))

Local $aArray2 = _ArrayDeclareFromString(StringMid($CmdLineRaw, StringInStr($CmdLineRaw, "[[")))

_ArrayDisplay($aArray2, "in script 2 : Array2", "", $ARRAYDISPLAY_COLALIGNCENTER)
if @error = 1 Then
    MsgBox($MB_TOPMOST, "Error", "Array2 is not an array")
endif

;~ #include <WinAPIShPath.au3>
;~ Local $aData = _WinAPI_CommandLineToArgv($CmdLineRaw)
;~ _ArrayDisplay($aData, '_WinAPI_CommandLineToArgv')

script2-a.jpg.3a411427954a219687017672a19b7668.jpgscript2-b.jpg.b3c62b8ccdbe7d82c8630d1e7ead443b.jpgscript2-c.jpg.bbf6b3b06151116ccb05bb393f7126ff.jpg

Something strange is why did $CmdLine[1] lose all his double-quotes, even _WinAPI_CommandLineToArgv() which is commented at the end of script 2 shows this ! Gladly the whole string, untouched, is found in the right part of $CmdLineRaw

In case you need to pass other variables between scripts, the syntax found in the following script may help you :
https://www.autoitscript.com/forum/topic/195073-autoit3executescript-variable-space-can-a-variable-defined-in-autoit-script-be-used-in-txt-file-autoit-code/?do=findComment&comment=1398965

Good luck :)

 

ArrayMultiDim.au3

 

Link to post
Share on other sites

Thanks everybody. I will read all your links/advices.
My first script is a tcp server and I would like a second script to handle the "data processing" by this server.
So realtime would be great.

 

EDIT : So I can't use AutoitObject because the 2 scripts will be compiled. Because of the realtime update, I can't use command line parameters neither.

The array to string function is interesting with _FileWriteFromArray.

Maybe Filemapping could be another solution :

Edit 2 : I can't make FileMapping to work with 2 compiled scripts. It works with one.

Edited by tatane
Link to post
Share on other sites

Hi all :)
This may interest the readers who (as me) never used until now a compiled script to run another script. Here is the 1st test I just did, based on the 2 scripts found in my precedent post ("script part1.au3" and "script part2.au3"). Actually this is also related to tatane's topic "Communication between 2 scripts"

1) Modify "script part1.au3", changing this :

$iPID = Run(@AutoItExe & " /AutoIt3ExecuteScript " & chr(34) & @ScriptDir & "\script part2.au3" & chr(34) & " " & $sStringFromArray)

To that :

If Not @Compiled Then
    $iPID = Run(@AutoItExe & " /AutoIt3ExecuteScript " & chr(34) & @ScriptDir & "\script part2.au3" & chr(34) & " " & $sStringFromArray)
Else
    $iPID = Run(chr(34) & @ScriptDir & "\script part2.exe" & chr(34) & " " & $sStringFromArray)
EndIf

2) Compile both scripts to script part1.exe and script part2.exe

3) To make sure no source file will interfere :
Rename script part1.au3, script part2.au3, the whole C:\Program Files\AutoIt3 directory to whatever

4) Double-clic on script part1.exe and everything works fine : the chaining from script part1.exe to script part2.exe works, the array is correctly displayed in script part2.exe as in precedent post :)

5) Revert part 3) to bring back the correct scripts & AutoIt3 directory names & locations.

Now it's time to do a 2nd test, with that #pragma directive, to "allow the compiled executable to execute non compiled scripts", also I'll test .a3x during chaining etc... but anyway, this 1st test "exe => exe" with parameters passed between both .exe did work

When the 2nd test will be done, I'll post the results here, in this same post (no need to create a new post for that). Let's hope I'll be as lucky in the 2nd test as I was in this 1st one :)

Edited by pixelsearch
Link to post
Share on other sites

The commandline idea is nice but while the OP's requirement of "1000 rows and 4 columns" might not exceed the maximum commandline length with some planning, the method would fail eventually with larger arrays including the element contents.

DDE however is true IPC and would not entail the user having to be careful of exceeding a buffer length.

Edited by Mobius

wtfpl-badge-1.png

Link to post
Share on other sites

@Mobius :
You're right, I tested the 2 precedent scripts on "big" arrays, to find where the limit was :

Local $aArray[555][4]
For $row = 0 to 554
    $aArray[$row][0] = $row & ":" & "0"
    $aArray[$row][1] = 528
    $aArray[$row][2] = 31
    $aArray[$row][3] = "HOSTNAME|1|02:45:47|abcdefgh|username|5"
Next

[555] rows are the limit in script 1, and $array2 will be ok in script 2 when [555] rows are populated like shown.
But if you try [556] rows or more,  an error will happen in script 2, _ArrayDisplay returning @error = 1 (not an array)

So before the error happened, I copied the long variable $CmdLineRaw in a file, to test its length and analyze how it ends :

FileWrite("c:\temp\test.txt", $CmdLineRaw)

Results show that if its length is > 32Kb (that's 556 rows or more) then the error happens because it's an unterminated string.

"AutoIt3 Limits/defaults" in help file shows a 32Kb limitation in these cases :
MAX_ENVSIZE 32767 Maximum size for an ENV variable.
WINTEXTBUFFER 32767 WM_GETTEXT randomly fails if > 32767

Good to know, sky isn't always the limit :)

 

Link to post
Share on other sites

Thanks for your time passed on looking at my demand.

Because of the frequent updating I finally chose a txt file save (write only  by the server and read only by the client).

I just convert the array to a string with the function made by jguinch.

Edited by tatane
Link to post
Share on other sites
  • 2 months later...
On 12/5/2018 at 6:10 AM, Mobius said:

DDE however is true IPC ......

...looking around found this posting and tried the DDE. Found that it does not like the server example compiled as 64 bit.
The client example work compiled as 32 and 64bit.
I don't know anything about DLL calls. Could you fix the UDF to make it 64bit friendly ?
Thanks  :) ( the emoji is to manipulate you emotionally into doing it )  

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By SEuBo
      Hi there,
      while I created an example script to generate and execute a function during runtime, I stumbled across a neat way to share data between running autoit scripts.
      This is done using the amazing magic of  AutoItObject_Internal . (You'll need at least Version 3.0.0 of AutoItObject_Internal)
      Using this UDF, you can create a shared data storage, basically an empty "AutoitObject_Internal-"Object which you can then use to write / read data Inline. no set/get methods, just
      #include "AutoItSharedData.au3" $oShare = _AutoIt_SharedData_CreateOrAttach("MyCustomID") $oShare.some_data = 'foo' and you're done. any other script accessing this data will have to do:
      #include "AutoItSharedData.au3" $oShare = _AutoIt_SharedData_CreateOrAttach("MyCustomID") ConsoleWrite($oShare.some_data & @LF)  
      Basically it's Larsj's Implementing IRunningObjectTable Interface, but you dont have a Dictionary, but an IDIspatch Object instead.
      There are already a bunch of IPC options available - and this is another one.
      AutoItSharedData.au3
       
      Example Script 1
       
      Example Script 2
      Output: 
      To test: run Example Script 1, Then run example Script 2.. or the other way around.
      Example Script 3
      Example_sharedata3.au3
      Example_sharedata3_Controlsend.au3
      Example_sharedata3_Tooltip.au3
      To test: run Example_sharedata3.au3.
      Output: 
       
       Example SharedData4:
      Output: 
      /Edit: Please note that there's a limitation with the Running object table :
      The Script accessing a variable first, will be the "server" for this variable. This means, access to that variable from other scripts should only be possible, as long the "server" script is running! Use appropriate Object Error handlers in case you don't want the surviving "clients" to crash.
      Feedback and/or improvements appreciated
       
      changelog
      version 2.0
      Removed need for AutoItObject, as AutoItObject_Internal now comes with ROT support Added UDF Header Fixed typo on "#include AutoItObjectInternal.au3" -> "#include AutoItObject_Internal.au3" Added ObjGet() after registering the object fails (in case 2 programs tried to register the same ID simultaneously) Updated Examples & zip archive. Cheers,
       
      AutoItSharedData.zip
    • By argumentum
      so in https://www.autoitscript.com/forum/topic/193254-solved-ipc-between-system-and-user/ I asked around about IPCs and got all the answers I was looking for.
      Now the question is: what IPC is most "resilient" on an overwhelmed PC, meaning, the CPU is at 100%, memory is top out and, as is always, need to rely on the IPC.
      ..and all this happened because I open over 100 GUIs at once 😜
      ..but it happens sporadically on low CPU or memory demand anyways.
      ..should I sleep() some time before running another instance ?
      I did not know if to make the question in technical, chat, ..or here. So it's here.   
      Since you will ask what I've tried, I've used the IPC from the Fork UDFish ( WM_COPYDATA that can do Admin/user mix ) and the FMIPC file mapping,  that work under the same conditions.
      So, how do you handle IPC if it fails ?
       
    • By argumentum
      I was in need of an IPC (Interprocess communication) between system, admin and user levels, and ended up writing this UDF to suit my wantings.
      Hope you find it useful too.
      Works from WinXP/Server2003 to the now current Win10/Server2016.
      It communicates between any mix of x32, x64, Admin, User.

      In the zip file, there is the UDF and an example: FMIPC(v0.2018.04.04).zip
      Special thanks to @RTFC for the help in the support forum   
    • By argumentum
      There is this topic on Examples about IPC. My question is: What is the best IPC to work with between a script running as SYSTEM level, User level, and Administrator level to interact with each other ? 
      Thanks
    • By JohnWIlling
      IPC_IO.AU3
      I am in the need for a simple synchronous Client/Server communication.  I found several examples and references to various kinds of Inter-Process Communications such as TCP, Named Pipes, Mail Slots, Shared Memory, Memory Mapped Files, and simple Files.  I wanted to see what the best solutions would be.  I began developing a library and slowly began adding each of the IPC methods and ended up with a library with a very simple synchronous “ASCII” API where the application can choose which method to use at startup.
      For the Server side, a Server app must initialize communication by calling:
      Func InitConnection($cType = $cDefaultType, $ResourceName = "", $bBlock = $cDefaultBlocking, $fSleepFunc = "", $iBufSize = $DEFAULT_BUFSIZE)
      The optional arguments allow the app to specify the connection type (such as: $cNamedPipe, $cFile, $cTCP, $cSharedMem, $cMailSlot), a value for the resource name (such as the file, named pipe name, TCP port number, etc.), the communication buffer size, and a callback function for when the “read” is waiting for data.
      A “File Descriptor” is returned and must be used in the future API calls.
      The Server side must then call:
      Func StartConnection($iFD)
      This call waits for a Client to connect.  The Server then calls:
                      Func ReadData($iFD, ByRef $sData)
      To read a Request from the Client and then calls:
                      Func WriteData($iFD, ByRef $sData)
      To send the reply back to the Client.
      When communication with the Client is done, the Server app will call:
      Func StopConnection($iFD)
      When the Server app is done with the communications it will call:
      Func EndConnection($iFD)
       
      For the Client side, a Client app must open the communication by calling:
      Func OpenConnection($cType = $cDefaultType, $ResourceName = "", $bBlock = $cDefaultBlocking, $fSleepFunc = "", $iBufSize = $DEFAULT_BUFSIZE)
      The optional arguments allow the app to specify the connection type (such as: $cNamedPipe, $cFile, $cTCP, $cSharedMem, $cMailSlot), a value for the resource name (such as the file, named pipe name, TCP port number, etc.), the communication buffer size, and a callback function for when the “read” is waiting for data.
      A “File Descriptor” is returned and must be used in the future API calls.
      The Client side then send a request to the Server app by calling:
                      Func WriteData($iFD, ByRef $sData)
      To read a Response from the Server by calling:
                      Func ReadData($iFD, ByRef $sData)
      To end the connection to the Server by calling:
      Func CloseConnection($iFD)
       
      Within the IPC_IO.AU3 library, each IPC method is ether:
      ·         “stream” based where data is read/written by calling _WinAPI_ReadFile/TCPRecv or _WinAPI_WriteFile/ TCPSend
      ·         “direct” based for Shared memory where the Client reads the data directly from the Server App’s memory and the Server directly reads the Client App’s memory
      In processing a request, the “ReadData” process starts by checking if data is ready to be read by calling the routine: “ReadStart”, then it reads in the size of the request by calling “ReadSize”, it then reads in the Ascii Request by calling “ReadBuffer”, then the sequence is completed by calling “ReadEnd”. 
      The Write Process follows the same sequence with “WriteData” calling “WriteStart”, “WriteSize”, “WriteBuffer”, “WriteEnd”.
       
      Results
      My testing showed that the performance of sending and receiving of a 10k file took:
      ·         "Shared Memory" was the fastest, at 0.007468 Sec
      ·         “Named Pipes” at 0.015954
      ·         “Mail Slots” at 0.016427
      ·         “File Based” at 0.270287
      ·         “TCP” at 0.994884
       
      IPC_IO.au3
      Client.au3
      Server.au3
×
×
  • Create New...