Jump to content

Howto avoid parallel I/O conflicts, setSerial ?


Recommended Posts

mutex afaik

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

slbmeh,

I believe _singleton uses either semaphores or a mutex.

kylomas

I wasn't familiar with the functionality of _Singleton until now, the name threw me off. I assumed that it would either only allow one instance of the application or queue execution of an application until the previous had completed. The documentation says Mutex.

The purpose of a mutex is to restrict the same piece of code or data from being used at the same time, whereas a binary semaphore would be more applicable in this situation because it ensures singular access to a specific resource. In a threaded application, or multiple instances of an application running parallel one would assume that the task can be executed on multiple resources simultaneously. Usage of a Mutex would limit execution of a function until the previous process has completed its task. A binary semaphore would instead allow for an application to execute the segment of code as long as the resource it is attempting to access were a different resource.

Link to comment
Share on other sites

slbmeh,

Yes, and thus the name "_singleton". This issue comes up frequently, especially with regard to serializing access to data. There are several disparate commonly suggested solutions. It would be to everyones benefit to develop a paradigm (a Valik attempted with _singleton) that provides the granularity that you suggest for "read", "write", "alter", and "delete" access. Unfortunately, I do not have the skills on this platform to do this, perhaps you do!

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hi Ed,

here is the code I use to make sure that scripts started in a bulk run serialized.

It's a modified and stripped down version of _Singleton. you can find the whole discussion.

;---------------------------------------------------------------------
; Make sure that only one script runs at a time
;---------------------------------------------------------------------
While 1
    If _SingletonEx("New Employee", 1) = 0 Then
        Sleep(2000)
    Else
        ExitLoop
    Endif
WEnd

; #FUNCTION# ====================================================================================================================
; Name...........: _SingletonEx
; Description ...: Enforce a design paradigm where only one instance of the script may be running.
; Syntax.........: _SingletonEx($sOccurenceName[, $iFlag = 0])
; Parameters ....: $sOccurenceName - String to identify the occurrence of the script.  This string may not contain the  character unless you are placing the object in a namespace (See Remarks).
;                 $iFlag          - Behavior options.
;                 |0 - Exit the script with the exit code -1 if another instance already exists.
;                 |1 - Return from the function without exiting the script.
;                 |2 - Allow the object to be accessed by anybody in the system. This is useful if specifying a "Global" object in a multi-user environment.
; Return values .: Success  - The handle to the object used for synchronization (a mutex).
;                 Failure   - 0
; Author ........: Valik
; Modified.......: water based on code modified by smartee
; Remarks .......: You can place the object in a namespace by prefixing your object name with either "Global" or "Local".  "Global" objects combined with the flag 2 are useful in multi-user environments.
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _SingletonEx($sOccurenceName, $iFlag = 0)

    Local Const $ERROR_ALREADY_EXISTS = 183
    Local $tSecurityAttributes = 0
    Local $handle = DllCall("kernel32.dll", "handle", "CreateMutexW", "struct*", $tSecurityAttributes, "bool", 1, "wstr", $sOccurenceName)
    If @error Then Return SetError(@error, @extended, 0)
    Local $lastError = DllCall("kernel32.dll", "dword", "GetLastError")
    If @error Then Return SetError(@error, @extended, 0)
    If $lastError[0] = $ERROR_ALREADY_EXISTS Then
        DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $handle[0])
        If @error Then Return SetError(@error, @extended, 0)
        If BitAND($iFlag, 1) Then
            Return SetError($lastError[0], $lastError[0], 0)
        Else
            Exit -1
        EndIf
    EndIf
    Return $handle[0]

EndFunc   ;==>_SingletonEx

'

Water,

I implemented your adaptation of _singleton, _singletonEx and wrote 2 functions around it to fit my needs,

; ----------------------------------------------------------------------------------------------------------------------------------
Func FileOpen_( $process = 'fileLock' )
; ----------------------------------------------------------------------------------------------------------------------------------
;  last updated    : 04/04/2012
;
; Serialize any process
;
;Debug_On_( $func )
; (
   ;Debug_( 'valid ' &$func &'( ' &$args &' )' )
   ; (
        Local $handle, $time
   ; )
   ;Debug_( 'perform ' &$func &'( ' &$args &' )', 'pause' )
   ; (
        $handle = 0
        $time    = 0
        ;
        While $handle = 0
            $time    = Sleep_( $time )
            $handle = SingletonEx_( $process, 1 )
        WEnd
        ;
        Assign( $process, $handle , 2 )
   ; )
;
EndFunc
; )
; ----------------------------------------------------------------------------------------------------------------------------------
#cs
Debug_On_ ( @ScriptName )
MsgBox ( 0, '', 'begintest' )
callS_ ( 'FileOpen_' )
MsgBox ( 0, '', 'endoftest' )
#ce
; ----------------------------------------------------------------------------------------------------------------------------------
Func FileClose_( $process = 'fileLock' )
; ----------------------------------------------------------------------------------------------------------------------------------
;  last updated    : 04/04/2012
;
; Normalize any process
;
;Debug_On_( $func )
; (
   ;Debug_( 'valid ' &$func &'( ' &$args &' )' )
   ; (
   ; )
   ;Debug_( 'perform ' &$func &'( ' &$args &' )', 'pause' )
   ; (
        DllCall( 'kernel32.dll', 'bool', 'CloseHandle', 'handle', Eval( String( $process ) ) )
   ; )
;
EndFunc
; )
; ----------------------------------------------------------------------------------------------------------------------------------
#cs
Debug_On_ ( @ScriptName )
MsgBox ( 0, '', 'begintest' )
callS_ ( 'FileClose_' )
MsgBox ( 0, '', 'endoftest' )
#ce

I hope I'm using it correctly ?

FileOpen_()
MsgBox( 0, '', 'Serial' )
FileClose_()
MsgBox( 0, '', 'Parralel' )
exit

I haven't tested it thoroughly but it seems to work perfectly, Thanks for helping me devellop this game bot !

Edited by EdDyreen

• Any number images • Images of any size • Any number of URLs • Any number of lines

Link to comment
Share on other sites

Looks good!

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...