RESOLVED - How to prevent the same script running twice

Stuart Beesley (Mr Toolbox)'s Avatar

Stuart Beesley (Mr Toolbox)

12 Feb, 2021 11:04 AM

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..?

Thanks

  1. 1 Posted by hleofxquotes on 12 Feb, 2021 06:36 PM

    hleofxquotes's Avatar

    Stuart, one cheap/easy way to create "lock handler" pattern:

    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.

    On starting

    • If you get a response, then stop
    • Otherwise, you can proceed to start

    A non-MD extension method,

    • A static variable.
    • OR a known file.

    For both, watch out for cleanup problem (the lock creator die without cleanup) which will then prevent the instance to start.

  2. 2 Posted by Stuart Beesley ... on 12 Feb, 2021 07:58 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    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?

    Regards Stuart

  3. 3 Posted by Stuart Beesley ... on 18 Feb, 2021 05:56 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    OK, so here is the method I have found when using Python and an explanation about how MD is executing Python scripts.

    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).
    — 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.

    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....

    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)....

    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...

    SO - how to detect and manage across all of these..?

    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...

    Hope this helps....

  4. 4 Posted by Stuart Beesley ... on 25 Feb, 2021 08:26 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    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.

  5. Stuart Beesley (Mr Toolbox) closed this discussion on 25 Feb, 2021 08:26 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac