Results 1 to 2 of 2

Thread: Threading in ASP

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Location
    Columbus, OH
    Posts
    3

    Threading in ASP

    I have never used threading in vbscript and recently got assigned a project to run a program on a 8 processor server. So in order to maximize the server I wanted to create 8 threads and run them simultaneously. Could anyone please send me a sample code to start from?

    Lets say the program is C:\xyz.exe and the ini file that needs to be passed to it is C:\xyz.ini

    The reason that I want to create 8 threads, is cause the data that needs to run has about 800,000 rows.

  2. #2

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Location
    Columbus, OH
    Posts
    3

    Smile Re: Threading in ASP

    I got some help from another site, but hopefully this can help someone over here.

    Code:
    Author: Jeff Price
    Description:
    Multi-threading agent to run parallel instances of your app/script/process/etc, in order to reduce the total
    run time of the desired process (eg, auditing 500+ systems). It also has a sentinel which will kill any
    errant threads after a specified timeout. The vbScript header has detailed info.
    
    Script:
    Option Explicit
    ' ==============
    Const MaxRunTimeMilliSec = 900000000 ' 15 minutes
    Const MaxForks = 4 'starts from zero (ie, 0 = one fork, 5 = six forks)
    ' ==============
    ' ThreadForker.vbs
    ' written by Jeff Price, , Dec-2001
    ' ==============
    '
    ' The basic code structure to get the forker working is
    '
    '
    ' 'redimension the thread tracker and reset the "time" to -1 seconds
    ' redim aThreadInfo( MaxForks, 1)
    ' for i = 0 to UBound( aThreadInfo, 1)
    ' aThreadInfo(i,1) = -1
    ' next
    ' <start loop>
    ' 'loop until we've a free process
    ' While GetNextThread = -1
    ' ProcessCheck
    ' Wend
    '
    ' iThreadNum = GetNextThread
    ' 'fire up your "script" in a thread
    ' set aThreadInfo(iThreadNum, 0) = wshShell.Exec( <command> )
    ' 'let the thread get settled
    ' wScript.Sleep 1000
    ' 'start the timeout counter
    ' aThreadInfo( iThreadNum, 1) = 1
    ' <loop return>
    ' 'keep going until all processes have finished.
    ' While ProcessCheck
    ' Wend
    '
    '
    ' If your <command> is command-prompt based I have found that you need to minimise the
    ' text output as it fills up a input buffer and causes it to hang
    '
    ' You need the "bin" subfolder with kill.exe. If you do not have the Windows NT resource kit, then
    ' download a process killer application and groom the 'wshShell.Run ".\bin\kill.exe.....' line
    ' appropriately.
    ' I tried to use the wshShell.Terminate method but it seem to not like non-Windows threads (eg, a batch/script)
    '
    ' You should setup your <command> string/script to accept the ThreadNum as a parameter and
    ' use to prefix all temp file names with the ThreadNum (eg, 0output.txt and 1output.txt). You
    ' could have unexpected results if your forked script use the same temp file as another fork.
    '
    ' The individual threads are tracked by the aThreadInfo(x,1) array and the ProcessCheck
    ' routine. aThreadInfo(x,0) is an object of wshScriptExec type and is returned by
    ' wshShell.Exec. aThreadInfo(x,1) is the time in seconds that the process has been running.
    '
    ' ==============
    ' ThreadForker variables
    dim wshShell, oFileSys
    dim aThreadInfo(), iThreadNum
    'initialise the core objects req'd
    Set wshShell = WScript.CreateObject("WScript.Shell")
    Set oFileSys=CreateObject("Scripting.FileSystemObject")
    'check that we've got kill.exe
    if NOT oFileSys.FileExists(".\bin\kill.exe") then
    wScript.Echo ""
    wScript.Echo "KILL.EXE missing from the '.\bin\' folder"
    wScript.Echo " kill.exe is part of the Windows NT Resource Kit, or download/use"
    wScript.Echo " an alternate process killer and groom this script appropriately."
    wScript.Quit
    End If
    'check we've atleast v5.6 of WSH
    if CDbl(wScript.Version) < CDbl("5.6") then
    wScript.Echo ""
    wScript.Echo " ***************** "
    wScript.Echo " VERSION ALERT"
    wScript.Echo ""
    wScript.Echo " This script requires atleast v5.6 of Windows Script Host."
    wScript.Echo ""
    wScript.Echo " Your current version is " & wScript.Version
    wScript.Echo ""
    wScript.Echo " http://msdn.microsoft.com/downloads/default.asp"
    wScript.Echo " -- Web Development"
    wScript.Echo " - Windows Script"
    wScript.Echo " --- Windows Script Host 5.6"
    wScript.Echo ""
    wScript.Echo "***************** "
    wScript.Echo ""
    wScript.Quit
    end if
    if InStr(LCase(wScript.FullName), "wscript.exe") then
    wScript.Echo "You have run this script from the GUI (wscript)" & vbCRLF & "Please rerun from a
    command prompt as" & vbCRLF & " 'cscript ThreadForker.vbs'"
    wScript.Quit
    End if
    '
    '
    '
    'Personal variables not needed by core thread-forker
    dim sServerList, i, sLine
    dim oServerFileList
    'redimension the thread tracker and reset the "time" to -1 seconds
    redim aThreadInfo( MaxForks, 1)
    for i = 0 to UBound( aThreadInfo, 1)
    aThreadInfo(i,1) = -1
    next
    ' a array looping example (change the "0" to a "1" to enable this section)
    if 0 then
    sServerList = Array("Server1", "Server2", "Server3","Server4", "Server5", "Server6")
    for i = 0 to UBound( sServerList, 1)
    'loop until we've a free process
    While GetNextThread = -1
    ProcessCheck
    Wend
    'get a Thread number
    iThreadNum = GetNextThread
    'run your custom command
    wScript.Echo "cmd /c srvinfo \\" & sServerList(i) & " > " & sServerList(i) & ".txt"
    set aThreadInfo(iThreadNum, 0) = wshShell.Exec( "cmd /c srvinfo \\" & sServerList(i) & " > " &
    sServerList(i) & ".txt")
    wScript.Echo "New ProcessID = " & aThreadInfo(iThreadNum, 0).ProcessID
    'let the process settle
    wScript.Sleep 2000
    'start the thread timer
    aThreadInfo( iThreadNum, 1) = 1
    next
    While ProcessCheck
    Wend
    End If
    ' a simple file looping example (change the "0" to a "1" to enable this section)
    if 0 then
    if oFileSys.FileExists ("ServerList.txt") then
    set oServerFileList = oFileSys.OpenTextFile("ServerList.txt")
    While NOT oServerFileList.AtEndOfStream
    sLine = oServerFileList.ReadLine
    if Trim(sLine) <> "" then
    While GetNextThread = -1
    'loop until we've a free process
    ProcessCheck
    Wend
    'get a Thread number
    iThreadNum = GetNextThread
    'run your custom command
    wScript.Echo "example.bat " & iThreadNum & " " & sLine
    set aThreadInfo(iThreadNum, 0) = wshShell.Exec( "example.bat " & iThreadNum & " " &
    sLine)
    wScript.Echo "New ProcessID = " & aThreadInfo(iThreadNum, 0).ProcessID
    'let the process settle
    wScript.Sleep 2000
    'start the thread timer
    aThreadInfo( iThreadNum, 1) = 1
    End If
    Wend
    oServerFileList.Close
    set oServerFileList = Nothing
    End if
    While ProcessCheck
    Wend
    End if
    wScript.Quit
    ' ==============
    ' return TRUE = yes we have active processes
    ' return FALSE = no active processes
    ' ==============
    Function ProcessCheck()
    dim i
    ProcessCheck = False
    wScript.Sleep 1500
    wScript.Echo ""
    for i = 0 to UBound( aThreadInfo, 1)
    'wScript.Echo "checking " & i & " with timeout " & aThreadInfo(i, 1)
    if aThreadInfo(i,1) > -1 then
    wScript.Echo "checking " & i & " with timeout " & aThreadInfo(i, 1) & " and status " & aThreadInfo
    (i,0).Status
    if aThreadInfo(i, 0).Status = 0 then
    if aThreadInfo(i,1) > MaxRunTimeMilliSec then
    wScript.Echo "terminating thread " & i & " and processid " & aThreadInfo(i,0).ProcessID
    'aThreadInfo(i,0).Terminate
    wshShell.Run ".\bin\kill.exe " & aThreadInfo(i,0).ProcessID, 2, True
    wScript.Sleep 1000
    set aThreadInfo(i,0) = Nothing
    aThreadInfo(i,1) = -1
    else
    ProcessCheck = True
    aThreadInfo(i, 1) = aThreadInfo(i, 1) + 1500
    End if
    Else
    aThreadInfo(i, 1) = -1
    set aThreadInfo(i, 0) = Nothing
    'wScript.Echo i, aThreadInfo(i, 1)
    End If
    End if
    next
    'wScript.Echo "active processes? :" & ProcessCheck
    End Function
    ' ==============
    ' ==============
    Function GetNextThread ( )
    dim i
    GetNextThread = -1
    for i = 0 to UBound( aThreadInfo, 1)
    'wScript.Echo "GetNextThread" & i & ":" & aThreadInfo(i, 1)
    if aThreadInfo( i, 1) = -1 then
    'aThreadInfo( i, 1) = 0
    GetNextThread = i
    i = UBound( aThreadInfo, 1) +1
    End if
    next
    End Function
    Last edited by fabiand; Sep 27th, 2007 at 09:13 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width