Sign in to follow this  
Followers 0
CodeMaster Rapture

How to Multi Process in AutoIt!

7 posts in this topic

Hey all,

I got bored and thought up a way to multi-thread AutoIt scripts. Keep in mind that this is a proof on concept and probably has a couple bugs in it.

A little about what is going on in the scripts:

1.) Our main thread (MutliThread Reader) will execute other compiled scripts.

2.) These compiled scripts do whatever they are designed to do and export thier data to a file

3.) Our main thread goes about its business and checks for changes in the thread data files periodically

4.) Our main thread also manages house keeping with the thread data files.

I haven't figured out how to pass arguments yet, but I'm sure there is a way.

MainThread.au3

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

 AutoIt Version: 3.2.11.1 (beta)
 Author:         CodeMaster Rapture

 Script Function:
    Executes and Reads multiple threads and parses information.

#ce ----------------------------------------------------------------------------
#include <GUIConstants.au3>
#include <Array.au3>

Global $GUI_Main = GUICreate("MultiThread Reader",300,300)
GUISetState(@SW_SHOW)

Global $a_Files,$a_TimeStamps ;These are paired arrays. I.E. $a_Files[1] contains $a_TimeStamps[1]

AdlibEnable("_PollThreads",250) ;Every 250ms

_CreateNewThread("NewThread.exe")
_CreateNewThread("NewThread.exe")
_CreateNewThread("NewThread.exe")
_CreateNewThread("NewThread.exe")

While 1
    Switch (GUIGetMsg())
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    Sleep(10)
WEnd

Func _CreateNewThread($sz_ThreadPath,$v_0 = "",$v_1 = "",$v_2 = "",$v_3 = "",$v_4 = "")
    If (@NumParams > 1) Then
        Local $a_Args[5] = [$v_0,$v_1,$v_2,$v_3,$v_4]
        ReDim $a_Args[@NumParams-1]
        $sz_Data = _ArrayToString($a_Args," ")
        ;Have not figured out a way to pass arguments yet...
    EndIf
    Local $PID = RUN($sz_ThreadPath)
    If (Not $PID) Then
        MsgBox(0,"Error","Failed to launch '" & $sz_ThreadPath & "'.")
    EndIf
    Sleep(1000)
EndFunc

Func _PollThreads()
    Local $iCount = 0, $sz_Filename = ""
    
    Local $h_FileSearch = FileFindFirstFile("Thread.*.dat")
    If ($h_FileSearch == -1) Then
        Return
    EndIf
    
    While 1
        $sz_Filename = FileFindNextFile($h_FileSearch)
        If (@error) Then
            ExitLoop
        EndIf
        
        Local $h_CurrentFile = FileOpen($sz_Filename,0)
        If ($h_CurrentFile == -1) Then
            ;MsgBox(0,"Error","Failed to open " & $sz_Filename & @CRLF & "Perhaps it is being written to?"
            ContinueLoop
        EndIf
        
        Local $sz_TimeStamp = FileReadLine($h_CurrentFile)
        ;Read other information here
        FileClose($h_CurrentFile) ;End of file reading, let's close it.
        
        ;Now parse it:
        If (StringInStr($sz_TimeStamp,"Dead",1)) Then ;You may want to do this before reading other information (Depending on amount, of course)
            ;Ensure FileClose($h_CurrentFile) has been called before deleting...
            FileDelete($sz_Filename)
            
            ;Ok, let's delete our records of this thread:
            Local $iInstance = _ArrayBinarySearch($a_Files,$sz_Filename)
            If ($iInstance <> "") Then
                _ArrayDelete($a_Files,$iInstance)
                If (@error == 2) Then
                    Global $a_Files ;If there is only one element left, redeclare it.
                EndIf
                _ArrayDelete($a_TimeStamps,$iInstance)
                If (@error == 2) Then
                    Global $a_TimeStamps ;If there is only one element left, redeclare it.
                EndIf
            EndIf
            ContinueLoop
        EndIf
        
        If (Not IsArray($a_Files)) Then ;Due to the above code, or first run, let's make these arrays.
            $a_Files = _ArrayCreate($sz_Filename)
            $a_TimeStamps = _ArrayCreate($sz_TimeStamp)
        Else
            Local $iInstance = _ArrayBinarySearch($a_Files,$sz_Filename)
            If ($iInstance == "" And @error == 3) Then
                _ArrayAdd($a_Files,$sz_Filename)
                _ArrayAdd($a_TimeStamps,$sz_TimeStamp)
            Else
                If ($a_TimeStamps[$iInstance] == $sz_TimeStamp) Then
                    ;Didn't change... do nothing?
                Else
                    $a_TimeStamps[$iInstance] = $sz_TimeStamp
                    ;Update information for other parts of your program based on this thread...
                EndIf
            EndIf
        EndIf
        Sleep(10)
    WEnd
EndFuncoÝ÷ Ø×°NÞi֮ݫ­¢+ØÌ´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´((Õѽ%ÐYÉÍ¥½¸è̸ȸÄĸġѤ(ÕÑ¡½Èè
½5ÍÑÈIÁÑÕÉ((MÉ¥ÁÐչѥ½¸è(%
ÉÑÌѡɹѡɥ±¸((´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´´(¥¹±Õ±ÐíU%
½¹ÍѹÑ̹ÔÌÐì)±¥¹± ÅÕ½Ðí}Q¡ÉUÁÑÅÕ½Ðì°ÈÔÀ¤íÙÉäÈÔÁµÌ()±½°ÀÌØíU%}5¥¸ôU%
ÉÑ ÅÕ½ÐíQ¡ÉÅÕ½ÐìµÀìÕѽ%ÑA%°ÌÀÀ°ÌÀÀ¤)±½°ÀÌØíU%} ÕÑѽ¹}UÁÑôU%
Ñɱ
ÉÑ    ÕÑѽ¸ ÅÕ½ÐíUÁÑQ¡ÉÅÕ½Ðì°ÈÀÀ°ÈÔÀ°ÄÀÀ°ÌÀ¤)U%MÑMÑÑ¡M]}M!=¤()±½°ÀÌØíÍé}¥±¹µôÅÕ½ÐìÅÕ½Ðì)}MÑÉÑUÀ ¤()]¡¥±Ä(%MÝ¥Ñ ¡U%Ñ5Í ¤¤($%
ÍÀÌØíU%}Y9Q}
1=M($$%}á¥Ð ¤($%
ÍÀÌØíU%}   ÕÑѽ¹}UÁÑ($$%}Q¡ÉUÁÑ ¤(%¹MÝ¥Ñ (%M±À ÄÀ¤)]¹()Õ¹}MÑÉÑUÀ ¤($ÀÌØíÍé}¥±¹µôÅÕ½ÐíQ¡É¸ÅÕ½ÐìµÀìÕѽ%ÑA%µÀìÅÕ½Ðì¹ÐÅÕ½Ðì(%%¡¥±á¥ÍÑÌ ÀÌØíÍé}¥±¹µ¤¤Q¡¸($%1½°ÀÌØí¡}¥±ô¥±=Á¸ ÀÌØíÍé}¥±¹µ°À¤($%% ÀÌØí¡}¥±ôô´Ä¤Q¡¸($$%5Í   ½à À°ÅÕ½ÐíÉɽÈÅÕ½Ðì°ÅÕ½Ðí½Õ¹ÕÁ±¥Ñ¥± ÅÕ½ÐìµÀìÀÌØíÍé}¥±¹µµÀìÅÕ½Ðì¤Õнձ¹½ÐÙÉ¥äÑ¥Ù¥Ñä¸á¥Ñ¥¹¸ÅÕ½Ðì¤($$%á¥Ð($%¹%($$($%1½°ÀÌØíÍé}Ñô¥±I1¥¹ ÀÌØí¡}¥±¤($%¥±
±½Í ÀÌØí¡}¥±¤($($%%¡MÑÉ¥¹%¹MÑÈ ÀÌØíÍé}Ñ°ÅÕ½Ðí±¥ÙÅÕ½Ð줤Q¡¸($$%5Í   ½à À°ÅÕ½ÐíÉɽÈÅÕ½Ðì°ÅÕ½ÐíQ¡¥µÁ½ÍÍ¥±¡Ì¡ÁÁ¹ÌÌìQݼ½Ñ¡MµA%ÉÉÕ¹¹¥¹üÌÌìüÌÌìÅÕ½Ðì¤($$%á¥Ð($%¹%(%¹%(%M±À ÄÀ¤)¹Õ¹()Õ¹}Q¡ÉUÁÑ ¤(%1½°ÀÌØí¡}¥±ô¥±=Á¸ ÀÌØíÍé}¥±¹µ°È¤(%% ÀÌØí¡}¥±ôô´Ä¤Q¡¸($%5Í ½à À°ÅÕ½ÐíÉɽÈÅÕ½Ðì°ÅÕ½Ðí
½Õ±¹½Ð½Á¸ÅÕ½ÐìµÀìÀÌØíÍé}¥±¹µ¤($$íá¥Ð($%IÑÕɸ(%¹%(%1½°ÀÌØíÍé}Q¥µMѵÀô5dµÀì5=8µÀìeHµÀìÅÕ½ÐìÅÕ½ÐìµÀì!=UHµÀì5%8µÀìÅÕ½ÐìèÅÕ½ÐìµÀìMµÀìÅÕ½Ðì±¥ÙÅÕ½ÐìµÀì
I1(%¥±]É¥Ñ ÀÌØí¡}¥±°ÀÌØíÍé}Q¥µMѵÀ¤(%¥±
±½Í ÀÌØí¡}¥±¤)¹Õ¹()Õ¹}á¥Ð ¤(%1½°ÀÌØí¡}¥±ô¥±=Á¸ ÀÌØíÍé}¥±¹µ°È¤(%% ÀÌØí¡}¥±ôô´Ä¤Q¡¸($%5Í    ½à À°ÅÕ½ÐíÉɽÈÅÕ½Ðì°ÅÕ½Ðí
½Õ±¹½Ð½Á¸ÅÕ½ÐìµÀìÀÌØíÍé}¥±¹µµÀì
I1µÀìÅÕ½Ðí±Ñµ¹Õ±±ä¸ÅÕ½Ðì¤($%á¥Ð(%¹%(%1½°ÀÌØíÍé}Q¥µMѵÀô5dµÀì5=8µÀìeHµÀìÅÕ½ÐìÅÕ½ÐìµÀì!=UHµÀì5%8µÀìÅÕ½ÐìèÅÕ½ÐìµÀìMµÀìÅÕ½ÐìÅÕ½ÐìµÀì
I1(%¥±]É¥Ñ ÀÌØí¡}¥±°ÀÌØíÍé}Q¥µMѵÀ¤(%¥±
±½Í ÀÌØí¡}¥±¤(%á¥Ð)¹Õ¹

The possibilities are endless IF you can find a way to pass args.

Enjoy!

Share this post


Link to post
Share on other sites



lol this is an obvious, we all know of diffrent ways arrojnd multi-threading.

Why don't you spend more time concentrating on basic spelling and less time making inane comments in threads. Your comment in this one has added absolutely nothing to the thread, as usual.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Forgive me if I am wrong, but this doesn't seem to be multithreading... it seems to be multiprocessing (if thats a word). Multithreading generally implies multiple threads running under the same process, correct? I'm not trying to rain on your parade, I just think a more appropriate thread title could be in order. Or perhaps I need an education, as I know almost nothing about multithreading.


Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

@CodeMaster Rapture

You can pass arguments with Run by simply adding them after the filename,

EG

Run( @programDir & '\AutoIt3\AutoIt3.exe" NewThread.au3')

which also shows that you don't need the scripts to be compiled to run one script from another, although my example assumes that AutoIt is installed on the PC where the program is running.

A good way to communicate between the running programs is to use messages, then the spawned program is a bit more like a thread.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

I came into this thread expecting actual multi-threading. The title of this thread should be changed.

-S


(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]

Share this post


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
Sign in to follow this  
Followers 0