Prevent duplicate launch from shortcut
Is there a good, canonical way to prevent a script from launching again if there is already an instance running? Specifically, I want to prevent the scenario where a user launches the script from the home screen via an app shortcut, then goes back to the home screen and launches it again.
Right now that causes duplicate instances of my script to run. My current method is to have the first instance put a small token in its directory which any other instance can look for and immediately return without running if it isn't the owner of that token. The token is removed by the first instance at exit. This requires quite a bit of careful negotiation and has many difficult edge cases...so I would prefer some simpler way to make sure only a single instance of the script can be running at any time.
Just revisiting this issue, since it's once again on my plate to try and find a working solution.
When we left off, I'd tried various attempts using different ideas discussed above. None of them are really successful. I can successfully prevent a new instance from trying to launch, keeping a token in the ui module which let's me know if an instance is already running, and which is cleared when the running instance is closed.
The trouble is still with what happens when a script is launched via a home screen shortcut.
In that case, the pykit_preflight.py script is in some way being run (it's not clear exactly when and how that occurs @omz...any hints?), and it clears out the sys.modules and globals before it runs the script. So even though the newly launched script sees that an instance is already running, and bails immediately, the previous instance is left in a broken state.
The clearing of the modules and globals means the existing instance is suddenly left with all of its module-level global data pointing at None, so it ends up raising an exception as soon as it tries to do anything.
So...I tried all sorts of ways to prevent or replace how pykit_preflight does it's thing to no avail. I even tried stashing away the globals for every module in my codebase and restoring them all. No luck.
Anyone have any ideas or better knowledge of how to prevent the clearing when a script is launched from the home screen shortcut, while pythonista is already open and running?
Iirc, @dgelessus figured out a way to bypass the preflight altogether. I can't find that thread right now, I think it involved hijacking a call that happened within preflight.. this might be an option in his pythonista_startup.
You probably know all this, but from another thread, here are some methods to avoid module and or variables getting cleared:
##ways to keep modules from clearing:
- for modules, delete file
import mymodule del mymodule.__file__
now mymodule is not removed from sys.modules
- create name staring with dunder__
import mymodule sys.modules['__mymodule']=sys.modules['mymodule']
- set file access to nonwritable
- place module in site-packages
##ways to keep global vars from being cleared
1.start variable with
2.add to pythonista_startup
for things like ui.Views, threads, which needs to reference itself, but does not need to be refernced externally, etc it is sufficient to simply add the var to an existing builtin module
retain_globalis another way.
For modules getting cleared out from under you, the only practical solution iirc is that every function must import what it needs. That's what stash does, and it can survive anything (I think including pythonista://). It is really tedious-- basically anything that can get called after your initial launch cannot rely on modules being imported at the file/module level. Instead, each def must import modules it uses. There is not a great way to check that you remembered everything.
- for modules, delete file
Take a look at (here](https://github.com/dgelessus/pythonista_startup/blob/master/_preflight_hook_experiment.py)
at line 39
The first let's you register hooks that get called very early in preflight. I suppose you could check for your UI, and if present, raise an Exception, thus killing preflight. (I'm not 100% sure that's work).
Option 2 uses the DirAllTheGlobals trick to patch pythonista_startup's dir to return everything in main -- since preflight checks if each variable is in pythonista_startup.
I'd think you could also create a custom importer, that forced all imports into pythonista_startup where it would be safe, or maybe have a system to "register" required imports/globals associated with an app, that can be cleared when app closes
Amazingly, yanking out just the "disable global clearing" part of that pythonista_startup magic worked to allow relaunch from the home shortcut.
So, the ugly tinkering is relatively short: replace the existing pythonista startup module with a new module object containing the globals for the current pythonista_startup module, and which returns all the globals in main when asked for its contents.
This will effectively short-circuit the preflight system from clearing out any globals, since it skips any which are inside the pythonista startup module itself.
Here's the shortened code just to solve this one issue, if anyone else is encountering it:
print(u"Executing pythonista_startup...") try: print(u"Preventing globals clearing...") import sys import types class SaveGlobals(types.ModuleType): import __main__ def __dir__(self): return dir(type(self).__main__) # THESE LINES MUST COME LAST. # Anything past this point is executed in the context of the old # pythonista_startup module, which may already be partially # garbage-collected. saved = SaveGlobals(__name__, __doc__) vars(saved).update(vars(sys.modules["pythonista_startup"])) sys.modules["pythonista_startup"] = saved del sys del types del SaveGlobals del saved print(u"Done preventing globals clearing.") except: # Catch everything import sys import traceback print(u"Swallowed exception:", file=sys.stderr) traceback.print_exc() print(u"Attempt to re-raise", file=sys.stderr) del sys del traceback raise print(u"Done executing pythonista_startup.")
how do you make a homescreen shortcut to a pythonista script?
@cvp thank you!
when you say:
Altough last version of Pythonista offers an easy way to add an home screen shortcut for an edited script
do you mean the first link above?
@jmv38 no, via wrench icon
Problem is that beta expires in 2 days...
@cvp actually it does work on the regular version.
Not sure why i didnt see that before.
@TPO Do you have this on a Github anywhere? It's useful.
@nfmusician there you go : https://github.com/TPO-POMGOM/Pythonista-utilities
(I currently lack time to migrate to Github other utilities and modules I have developped for Pythonista, will do it as soon as time allows)