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.
-
Take a look at (here](https://github.com/dgelessus/pythonista_startup/blob/master/_preflight_hook_experiment.py)
And also
Here
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
-
@JonB thanks!
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.")
-
@shinyformica hello!
how do you make a homescreen shortcut to a pythonista script?
thanks!
-
last edited by cvp
-
@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.
Thanks.
-
@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)