tag:infinitekind.tenderapp.com,2009-01-14:/discussions/moneydance-development/4761-how-to-prevent-the-same-script-running-twiceInfinite Kind: Discussion 2021-02-25T20:27:14Ztag:infinitekind.tenderapp.com,2009-01-14:Comment/490302442021-02-12T11:04:15Z2021-02-25T20:27:14ZRESOLVED - How to prevent the same script running twice <div><p>Can anyone advise how in Python (Jython) or Java how I can detect whether my script (or extension) is currently running in memory and hence not allow a second instance to run..?</p>
<p>Thanks</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/490302442021-02-12T18:36:23Z2021-02-12T18:36:55ZRESOLVED - How to prevent the same script running twice <div><p>Stuart, one cheap/easy way to create "lock handler" pattern:</p>
<p>For MD extension, you can implement a status query in your extension where the 2nd instance can do sayHello() via the MD standard call URL pattern.</p>
<p>On starting</p>
<ul>
<li>If you get a response, then stop</li>
<li>Otherwise, you can proceed to start</li>
</ul>
<p>A non-MD extension method,</p>
<ul>
<li>A static variable.</li>
<li>OR a known file.</li>
</ul>
<p>For both, watch out for cleanup problem (the lock creator die without cleanup) which will then prevent the instance to start.</p></div>hleofxquotestag:infinitekind.tenderapp.com,2009-01-14:Comment/490302442021-02-12T19:58:42Z2021-02-12T19:58:42ZRESOLVED - How to prevent the same script running twice <div><p>Thanks. I get the concepts. I’ll have to play with code. Are you able to share a static variable example of code? I just don’t know how to check for that between scripts? Ie surely they exist in their own ‘scope’s?</p>
<p>Regards Stuart</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/490302442021-02-18T17:56:20Z2021-02-25T20:24:33ZRESOLVED - How to prevent the same script running twice <div><p>OK, so here is the method I have found when using Python and an explanation about how MD is executing Python scripts.</p>
<p>When using Moneybot to RUN a script, then a new namespace is created for that script . Basically a new python interpreter instance is created and .execfile() is executed on your script (every time).<br>
— Thus if you run your script multiple times using this method, they will all have their own empty / new namespace(s)... So in theory, this is actually OK and you won't get namespace contamination.</p>
<p>Now the odd thing is that the run_snippet area seems to be able to reference objects created in the Run name space(s), so it must be a parent / child thing... It calls .exec(scriptCode). It doesn't work the other way around. Also if you run two Moneybots and then exit one, then it zaps the name space of the other one....</p>
<p>When you install a script as an extension using the script_info, "type" = "menu", method; then when you select the extension from the extension menu you get a new name space created which continues to exist no matter how many times you call the extension... Hence if you re-run the script/menu, then the name space will be full of whatever you left last time - so be careful. If you run your extension twice and it's still running as a JFrame, then all sorts of bad things can happen (depending on how you have set you code and variables up)....</p>
<p>Lastly if you install a script as an extension, by bypassing the script_info, "type" = "menu" method so that your script installs (upon MD launch by executing .execfile()) as a run time extension / Feature Module, then of course, the py interpreter and name space will also continue to exist, along with your own classes that you created at run time... But this should be OK, as you want to set up your program to capture .invoke() events when the extension menu is clicked, and handle it within your code...</p>
<p>SO - how to detect and manage across all of these..?</p>
<p>The answer is to leverage JFrame().. These of course are pure Java and exist in the jvm namespace and are accessible to python through JFrame.getFrames()... if you extend JFrame and add some data that you control, then at script launch you can query JFrame.getFrames() to look for your own script's frames and then go from there. My own method is simply to detect my own frame, give it focus (or raise an exception, or dispose()) and then exit the script, BEFORE any other variable / object assignments take place...</p>
<p>Hope this helps....</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/490302442021-02-25T20:26:44Z2021-02-25T20:26:44ZRESOLVED - How to prevent the same script running twice <div><p>As an addendum, your extended JFrame() can of course hold any variables that your script needs instead of using ‘global’ variables as these risk being altered.</p></div>Stuart Beesley (Mr Toolbox)