omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    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?

    Pythonista
    5
    10
    6674
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • pacco
      pacco last edited by

      Specifically:

      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?

      Thanks,<br>
      Pacco

      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        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

        1 Reply Last reply Reply Quote 0
        • zencuke
          zencuke last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • ccc
            ccc last edited by

            You might check out scene.SceneView.paused http://omz-software.com/pythonista/docs/ios/scene.html#scene.SceneView

            1 Reply Last reply Reply Quote 0
            • omz
              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
              
              1 Reply Last reply Reply Quote 0
              • pacco
                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)

                Thanks,<br>
                Pacco

                1 Reply Last reply Reply Quote 0
                • zencuke
                  zencuke last edited by

                  It looks like @omz's is_in_background is in the new beta. The release notes mention

                  console.is_in_background()

                  sk.Scene has new methods did_pause() and did_resume.

                  I assume like the other background methods they also don't work in SceneView but the release note doesn't say.

                  1 Reply Last reply Reply Quote 0
                  • zencuke
                    zencuke last edited by

                    Oops. Forgot to markup the code and underscores got crunched. I wonder what underscore means in markdown? I guess I should look it up.

                    1 Reply Last reply Reply Quote 0
                    • dgelessus
                      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

                      1 Reply Last reply Reply Quote 0
                      • ccc
                        ccc last edited by

                        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!

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors