Page 1 of 2
1282 Simultaneous script execution corrupts variables [6404]
Posted: Fri Jul 09, 2010 7:27 am
by chrisjj
If I launch a script and then before completion launch it again, the variable writes within the second instance overwrite the first instance's variables.
Tracked at
http://www.ventismedia.com/mantis/view.php?id=6404 .
Re: 1282 Simultaneous script execution corrupts variables
Posted: Wed Aug 04, 2010 5:30 am
by chrisjj
At
http://www.ventismedia.com/mantis/view.php?id=6404 Jiri wrote:
It's actually designed this way to prevent problems with several instances of scripts running - there's always only one instance of a script.
and I replied
> It's actually designed this way to prevent problems
> with several instances of scripts running
It doesn't prevent problems AFAICS. It causes them.
> there's always only one instance of a script.
Not true. I can launch two instances and both show on the status bar.
Am I the only one finding I can launch two instances of a script and both show on the status bar?
Re: 1282 Simultaneous script execution corrupts variables
Posted: Wed Aug 04, 2010 7:16 am
by trixmoto
I know that I can, and I design my scripts to account for this.
Re: 1282 Simultaneous script execution corrupts variables
Posted: Wed Aug 04, 2010 7:53 am
by chrisjj
> I know that I can
Jiri, if you are reading this, please note: I think your statement is in error. And so I hope you will remove your RESOLVED status from this serious issue.
> and I design my scripts to account for this.
I have a scripting using many Dim variables that get corrupted by subsequent simultaneous instance. What's the best way allow two to run without this corruption? Failing which, to prevent two from running? Thanks.
Re: 1282 Simultaneous script execution corrupts variables
Posted: Wed Aug 04, 2010 10:09 am
by trixmoto
By "I know that I can" I was referring the fact that a script could be run twice. They are run within the same instance as you have discovered because the global variables are the same references, therefore Jiri is correct in what he has said.
I only use global variables for constants and I store all global information in a dictionary object in "SDB.Objects". Sometimes I define a lot of global variables, for instance one for each of the settings I've stored in "MediaMonkey.ini", but I read them in from the ini file (or update them from the dictionary object) at the beginning of each Sub to make sure they are correctly populated.
The first thing a number of my scripts (such as "Genre Finder") do is check to see if SDB.Objects is already populated with a relevant dictionary and exits if it does.
Re: 1282 Simultaneous script execution corrupts variables
Posted: Wed Aug 04, 2010 12:59 pm
by chrisjj
> By "I know that I can" I was referring the fact that a script could be run twice. They are run within the same instance
Ah. You actually said that in response to my
"Am I the only one finding I can launch two instances of a script ..."
> I only use global variables for constants and I store all global information in a dictionary object in "SDB.Objects".
Wow. That would really complicate and obfuscate my code. Likewise moving globals into locals, passing by reference.
Re: 1282 Simultaneous script execution corrupts variables
Posted: Wed Aug 04, 2010 1:54 pm
by trixmoto
I guess it depends on the interpretation of "instance", I wasn't thinking technically when I first responded. I always use SDB.Objects, seems to work well for me.
Re: 1282 Simultaneous script execution corrupts variables
Posted: Wed Aug 04, 2010 2:05 pm
by chrisjj
> I always use SDB.Objects, seems to work well for me.
Thanks for the suggestion.
How would code using Dictionary objects this for example:
Code: Select all
Dim global1, global2, global3
...
global2 = "string2"
global3= "string3"
...
global1 = global2 & global3
?
Re: 1282 Simultaneous script execution corrupts variables
Posted: Thu Aug 05, 2010 7:27 am
by trixmoto
Code: Select all
Dim dict : Set dict = CreateObject("Scripting.Dictionary")
dict.Item("global2") = "string2"
dict.Item("global3") = "string3"
dict.Item("global1") = dict.Item("global2")&dict.Item("global3")
Set SDB.Objects("MyDictionary") = dict
Re: 1282 Simultaneous script execution corrupts variables
Posted: Thu Aug 05, 2010 9:27 am
by chrisjj
Thanks, but surely still 'dict' may get corrupted by this problem. And that corruption will then get transferred to SDB.Objects.
Re: 1282 Simultaneous script execution corrupts variables
Posted: Thu Aug 05, 2010 2:21 pm
by trixmoto
Sorry, maybe I should have given you a little more...
Code: Select all
Dim dict : Set dict = SDB.Objects("MyDictionary")
If Not (dict Is Nothing) Then
Exit Sub
End If
Set dict = CreateObject("Scripting.Dictionary")
Set SDB.Objects("MyDictionary") = dict
dict.Item("global2") = "string2"
dict.Item("global3") = "string3"
dict.Item("global1") = dict.Item("global2")&dict.Item("global3")
Re: 1282 Simultaneous script execution corrupts variables
Posted: Thu Aug 05, 2010 8:18 pm
by chrisjj
Still corruption can occur e.g.
Code: Select all
Execution 1: dict.Item("global3") = "string3"
Execution 2: Set dict = CreateObject("Scripting.Dictionary")
Execution 1: dict.Item("global1") = dict.Item("global2")&dict.Item("global3")
Execution 1 will now find dict empty instead of containing the three strings.
Re: 1282 Simultaneous script execution corrupts variables
Posted: Fri Aug 06, 2010 3:10 am
by trixmoto
Theoretically, but unless your computer is one of those full room sized ones from the the 80s you don't have a hope in hell's chance of running the script a second time quick enough if that code is at the beginning.
Re: 1282 Simultaneous script execution corrupts variables
Posted: Fri Aug 06, 2010 5:08 am
by chrisjj
trixmoto wrote:you don't have a hope in hell's chance of running the script a second time quick enough if that code is at the beginning.
And if it is at another point?? Or points?? e.g. being in a SUB called multiple times?? Or between the set and get of the variable are many lines of other code e.g. doing slow things like file I/O??
I would like to hear from Jiri if he believes it is at all possible to use global variables without this risk of corruption.
Re: 1282 Simultaneous script execution corrupts variables
Posted: Fri Aug 06, 2010 7:18 am
by trixmoto
If you put it right at the beginning of each access point to your script then the script can only be running once within it's instance (as each subsequent to run will immediately exit out) and therefore there will be no corruption. Then at every exit point from your script you need to make sure that you set the SDB.Object back to "Nothing" so that the script then be run again.