#157085 - 2006-02-13 06:07 PM
Re: Multiple threads in KiX?
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Here is a simple example of multiple processes.
The parent spawns copies of itself, passing $iChildID as a parameter on the command line.
The child processes communicate to the parent via the INI file - in this case they just increment a simple counter:
Code:
Break ON $sINI=".\Thread.ini" $iChildMax=10 If $iChildID Srnd(@TICKS*$iChildID) $iSleepTime=RND(10)+2 "This is child # "+$iChildID+" sleeping for "+$iSleepTime+" seconds..."+@CRLF Sleep $iSleepTime $=WriteProfileString($sINI,"MAIN","COUNTER",1+CInt(ReadProfileString($sINI,"MAIN","COUNTER"))) "Child #"+$iChildID+" exiting."+@CRLF Exit 0 EndIf $=WriteProfileString($sINI,"MAIN","COUNTER",0)
For $i = 1 To $iChildMax "Starting child #"+$i+@CRLF RUN "kix32.exe "+@SCRIPTNAME+" $$iChildID="+$i Next $iChildCount=CInt(ReadProfileString($sINI,"MAIN","COUNTER")) While $iChildCount<$iChildMax "Waiting for children to exit, count is: "+$iChildCount+@CRLF Sleep 1 $iChildCount=CInt(ReadProfileString($sINI,"MAIN","COUNTER")) Loop "All child processes exited, parent closing."+@CRLF Exit 0
|
Top
|
|
|
|
#157086 - 2006-02-13 07:05 PM
Re: Multiple threads in KiX?
|
thepip3r
Hey THIS is FUN
Registered: 2005-03-02
Posts: 350
|
The Matrix/KiX things is awesome but I have a couple of questions on how to get each thread to run what I want it to. First off, this is the piece of code in question that spawns new threads correct? Code:
If $avServers[3,$i]<0.0001 AND $avServers[3,$i]>-0.0001 $gNull=WriteProfileString($gINI,$avServers[0,$i],"Status","9Poll Request") Run $gCOMSPEC+" "+$gKIX+' "'+@SCRIPTDIR+"\"+@SCRIPTNAME+'" $$sServer="'+$avServers[0,$i]+'"' Else
If that's the case, I see how you're kicking off a new thread but how to you tell that thread to perform what you want it to? Like for mine, I'd probably kick off a new inst of 10 folders but how does that new KiX file know what commands to run?
Edit: Sorry for the post. I started writing this like an hour ago and JUST posted it. I didn't see you post before and it will probably answer some of my questions.
Edited by thepip3r (2006-02-13 07:07 PM)
|
Top
|
|
|
|
#157087 - 2006-02-13 07:21 PM
Re: Multiple threads in KiX?
|
thepip3r
Hey THIS is FUN
Registered: 2005-03-02
Posts: 350
|
When I run your script, this is what I get, is this what it's supposed to say??
Quote:
Starting child #1 Starting child #2 Starting child #3 Starting child #4 Starting child #5 Starting child #6 Starting child #7 Starting child #8 Starting child #9 Starting child #10 Waiting for children to exit, count is: 0
ERROR : failed to find/open script [test.kix]!
ERROR : failed to find/open script [test.kix]!
ERROR : failed to find/open script [test.kix]!
ERROR : failed to find/open script [test.kix]!
ERROR : failed to find/open script [test.kix]! Waiting for children to exit, count is: 0
ERROR : failed to find/open script [test.kix]!
ERROR : failed to find/open script [test.kix]!
ERROR : failed to find/open script [test.kix]!
ERROR : failed to find/open script [test.kix]!
ERROR : failed to find/open script [test.kix]!
|
Top
|
|
|
|
#157088 - 2006-02-13 08:04 PM
Re: Multiple threads in KiX?
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Errr..no.
How are you running the script?
It calls itself, but I didn't include any path information in the RUN statement so it assumes that your current working directory is the directory that the script is in.
You could chuck @SCRIPTDIR in if you want to make it bullet proof.
Change the "RUN" line to read: Code:
RUN 'kix32.exe "'+@SCRIPTDIR+'\'+@SCRIPTNAME+'" $$iChildID='+$i
The output should actually look something like this:
Quote:
C:\Temp>kix32 thread.kix Starting child #1 Starting child #2 Starting child #3 Starting child #4 Starting child #5 Starting child #6 Starting child #7 Starting child #8 Starting child #9 Starting child #10 Waiting for children to exit, count is: 0 This is child # 10 sleeping for 11 seconds... This is child # 2 sleeping for 8 seconds... This is child # 5 sleeping for 12 seconds... This is child # 6 sleeping for 10 seconds... This is child # 7 sleeping for 8 seconds... This is child # 8 sleeping for 6 seconds... This is child # 3 sleeping for 6 seconds... This is child # 1 sleeping for 10 seconds... This is child # 4 sleeping for 4 seconds... This is child # 9 sleeping for 5 seconds... Waiting for children to exit, count is: 0 Waiting for children to exit, count is: 0 Waiting for children to exit, count is: 0 Waiting for children to exit, count is: 0 Child #4 exiting. Waiting for children to exit, count is: 1 Child #9 exiting. Waiting for children to exit, count is: 2 Child #8 exiting. Child #3 exiting. Waiting for children to exit, count is: 4 Waiting for children to exit, count is: 4 Child #2 exiting. Child #7 exiting. Waiting for children to exit, count is: 6 Waiting for children to exit, count is: 6 Child #6 exiting. Child #1 exiting. Waiting for children to exit, count is: 8 Child #10 exiting. Waiting for children to exit, count is: 9 Child #5 exiting. All child processes exited, parent closing.
C:\Temp>
|
Top
|
|
|
|
#157092 - 2006-02-23 11:13 AM
Re: Multiple threads in KiX?
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
Is there a way to pass a variable to one of the child scripts though?
Yes, the example that I provided you passes a variable on the command line. Any simple variable (number, string) can be passed this way.
You *cannot* pass a complex variable like an array or an object. You would need to pass a string reference to the object so that the child process can create a new local copy. for itself. Arrays would have to be converted to a string form and passed on the command line, by file or registry.
Quote:
The example I'm trying to think of is say you have a function that takes 4 seconds to return results. But you have to run that function on 500 objects so is there a way to throw that function into like a completely seperate kix file and then call that file but pass the current object name to it so you'd theoretically have 500 windows open all running the function on the same computer name?
Yes, in the simple example above all the threads are running in parallel. Once they have started they communicate their state with the parent process by using the INI file. It would be trivial to expand the example to make this two-way communication.
You could also communicate via the registry if you didn't want to use INI files.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 466 anonymous users online.
|
|
|