This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Way for your code to tell if Pythonista is not the foreground app?
pacco last edited by
I've been writing small test servers in Pythonista and connect to them using Safari or some other client app on the same device. I've noticed that network performance of the server is impacted pretty heavily if it tries to print anything to the console while it's running in the background. While one obvious solution is to just write all messages to a logfile instead of the console, I was wondering if there are any iOS methods surfaced in Pythonista to tell if it's running in the foreground/background?
You could try putting up a scene.Scene and then monitor pause(), resume(), and stop() omz-software.com/pythonista/docs/ios/scene.html#scene.Scene.pause
Unfortunately that doesn't help if using SceneView under ui. The following limitation is noted for SceneView:
The Scene.pause(), Scene.resume() and Scene.stop() methods will not be called automatically.
Looks like Scene has to be invoked with run() to get these. It is puzzling that there isn't more general background detection support.
You might check out scene.SceneView.paused http://omz-software.com/pythonista/docs/ios/scene.html#scene.SceneView
omz last edited by
If you're in the beta (won't work with the current App Store version), here's a function you could use. To be honest, I made this mostly for fun, I'll probably add an easier way to determine the state of the app.
def is_in_background(): from ctypes import cdll, c_void_p, c_char_p, c_int c = cdll.LoadLibrary(None) c.sel_registerName.restype = c_void_p c.sel_registerName.argtypes = [c_char_p] c.objc_getClass.argtypes = [c_char_p] c.objc_getClass.restype = c_void_p UIApplication = c.objc_getClass('UIApplication') c.objc_msgSend.argtypes = [c_void_p, c_void_p] c.objc_msgSend.restype = c_void_p app = c.objc_msgSend(UIApplication, c.sel_registerName('sharedApplication')) c.objc_msgSend.restype = c_int state = c.objc_msgSend(app, c.sel_registerName('applicationState')) return state != 0
pacco last edited by
Thanks for the feedback all. I even appreciate omz chiming in -- I did suspect it might take some ctypes-fu to determine an app's state but thought there might have been an easier way.
Clearly, this isn't a high-priority issue as I suspect running server apps on Pythonista isn't a huge use case. But it does make me wonder why console printing would slow things down? Is it simply because the app is in a backgrounded state or is it because it's offscreen? (i.e., console output has to be written to some sort of image backing store that's slower than a live render, etc)
It looks like @omz's
is_in_backgroundis in the new beta. The release notes mention
sk.Scenehas new methods
I assume like the other background methods they also don't work in SceneView but the release note doesn't say.
Oops. Forgot to markup the code and underscores got crunched. I wonder what underscore means in markdown? I guess I should look it up.
dgelessus last edited by
Underscores are like asterisks (
*) and can be used to make text italic (
_italic_) or bold (
__bold__). This unfortunately means that if you don't know about (or are too lazy to use ;) backticks to enclose code, Python names will often receive unwanted formatting: some_random_object.init
Nice additions in the new Pythonista Beta released last night to enable more robust detection of pause/resume and to keep servers apps running forever. Awesome turnaround time @omz!