Jump to content

Recommended Posts

Posted (edited)

This is a UDF (User defined functions) library for AutoIt, providing a simple way to start sub processes and communicate with them.

You can also find the AutoIt-IPC UDF on Github.

The UDF can be used to detect, if the script is started as a sub process by the main process and start the corresponding routines.
The UDF also provides the ability to send commands as well as data between the main and the sub process.

The IPC-Example files showcase different ways to use the UDF.
The only required file to be included in the script is the "IPC.au3".

The following functions are available:

; #CURRENT# =====================================================================================================================
; __IPC_StartUp
; __IPC_Shutdown
; __IPC_GetScriptExecutable
; __IPC_StartProcess
; __IPC_SubGetPID
; __IPC_SubCheck
; __IPC_SubConnect
; __IPC_SubSend
; __IPC_MainSend
; __IPC_ProcessStop
; __IPC_SubProcessing
; __IPC_MainProcessing
; __IPC_Log
; ===============================================================================================================================

Simple example on how to send commands with data between the main and sub process:

#cs ----------------------------------------------------------------------------

     AutoIt Version: 3.3.18.0
     Author:         Kanashius

     Script Function:
        Example script for the IPC InterProcessCommunication UDF.
        This example shows how commands can be sent and received.
        This includes commands with additional data.

#ce ----------------------------------------------------------------------------
#include "IPC.au3"

Global Const $iCOMMAND_START = 1, $iCOMMAND_END = 2, $iCOMMAND_PROGRESS = 3, $iCOMMAND_UNKNOWN = 4

; check if the call is a sub process and start the respective function
__IPC_SubCheck("_SubProcess", "_MainProcess")
If @error Then __IPC_Log($__IPC_LOG_ERROR, "__IPC_SubCheck: "&@error&":"&@extended)

; main/sub process both should call shutdown before exit
__IPC_Shutdown()
Exit
; registered as callback in __IPC_StartProcess to be called when data from the sub process is received
; the main process main method, registered in __IPC_SubCheck to be called when the script is running as main process (no sub process command line arguments detected)
Func _MainProcess()
    ; start a sub process calling the same script.
    ; the _CallbackMain method is called for messages received from the sub process
    ; 100 is the parameter provided to the sub process (total items)
    Local $hProcess = __IPC_StartProcess("_CallbackMain", "11")
    ; wait for the sub process to finish
    While ProcessExists(__IPC_SubGetPID($hProcess)) And Sleep(10)
    WEnd
EndFunc

; registered as callback in __IPC_StartProcess to be called when data from the sub process is received
Func _CallbackMain($hSubProcess, $data, $iCmd)
    ; $hSubProcess can be used to differentiate between different sub processes (if multiple are started with the same callback method)
    ; $data can be a string or binary data, depending on the data sent by the sub process
    ; $iCmd contains the command send by the server
    Switch $iCmd
        Case $iCOMMAND_START
            ConsoleWrite("Start processing "&$data&" items"&@crlf)
        Case $iCOMMAND_END
            ConsoleWrite("Finished processing"&@crlf)
        Case $iCOMMAND_PROGRESS
            Local $iTotal = Int(BinaryMid($data, 1, 4)) ; int values are 32bit=>4byte
            Local $iItemsDone = Int(BinaryMid($data, 5, 4)) ; int values are 32bit=>4byte
            Local $iPerc = ($iItemsDone=$iTotal)?100:Mod($iItemsDone, $iTotal)
            ConsoleWrite("Progress: "&$iItemsDone&"/"&$iTotal&" = "&Round($iItemsDone/$iTotal, 2)&" => "&$iPerc&"%"&@crlf)
        Case Else
            ConsoleWrite("Unknown command ["&$iCmd&"]: "&$data&@crlf)
    EndSwitch
EndFunc

; the sub process main method, registered in __IPC_SubCheck to be called when the script is running as a sub process
Func _SubProcess($hSubProcess)
    Local $iTotalItems = 10
    If UBound($CmdLine)>1 Then $iTotalItems = Int($CmdLine[1])
    ConsoleWrite("Process ["&$hSubProcess&"]: Start processing items: "&$iTotalItems&@crlf)
    __IPC_SubSend($iCOMMAND_START, $iTotalItems)
    If @error Then __IPC_Log($__IPC_LOG_ERROR, "Failed sending", @error, @extended) ; to check for errors when sending
    For $i=0 to $iTotalItems-1
        __IPC_SubSend($iCOMMAND_PROGRESS, Binary($iTotalItems)&Binary($i+1))
    Next
    __IPC_SubSend($iCOMMAND_END, "") ; to sent only a command, make $data empty. Otherwise, the command will be sent as data
    __IPC_SubSend($iCOMMAND_UNKNOWN, "some command")
    ConsoleWrite("Process ["&$hSubProcess&"]: Finished"&@crlf)
EndFunc

Changelog:

Version 1.0.0
- release of the first version

I hope you find this UDF useful and please leave a comment, if you have any suggestions to make it better or if you found any bugs.

Thank you to everyone who helps to improve the UDF :)

Download everything as a zip file: Github AutoIt-IPC download

IPC.au3 IPC-Example-Commands.au3 IPC-Example-Data.au3 IPC-Example-Gui.au3 IPC-Example-LargeData.au3 IPC-Example-ManualHandling.au3 IPC-Example-Mixed.au3 IPC-Example-SeparatedMain.au3 IPC-Example-SeparatedSub.au3

Edited by Kanashius
Posted
6 hours ago, argumentum said:

Why not just point the download to GitHub's main.zip

Hey, good idea, I added the link to the github zip file.
I want the UDF to be downloadable without the examples, so that was the main reason.
With the github zip download, I will leave it like this for now.
Maybe use the download counter as a little indicator, how most probably use the UDF :) 

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
×
×
  • Create New...