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.


    correct way to do threading in pythonista

    Pythonista
    6
    11
    9745
    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.
    • bitbucket158
      bitbucket158 last edited by

      I created a scene-based script in pythonista. I'd like to have a worker thread update some data periodically. The idea would be that the worker could download some data from a web site (via urllib) every so often and update member variables of a class which would then draw the results in its normal draw callback.

      I first tried the thread module and thread.start_new_thread. As an initial test my thread function looked similar to the following (going off memory now).

      def MyThreadFunc(self):
      while True:
      someVar += 1
      time.sleep(1.0)

      My expectation was that about every second the number would increment in the display. It didn't do this, though. It would seem to update sometimes, then go for a while before updating again. Like the timing was random.

      I then thought I'd try the threading module. I derived a class from Thread, created an instance, called start and had similar code to the above. This time it hung pythonista.

      So what is the best way to run a worker thread in the background with a loop similar to whats shown above?

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

        For the crash, maybe it's the problem described here:
        http://omz-software.com/pythonista/forums/discussion/comment/

        For the other problem, can you post a complete example?

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

          That link doesn't appear to work. Anyway, I went back and did some experimentation with threading in pythonista again today with no luck. Now, I'm not even getting that irregular update behavior, it seems anything I try, whether with the thread or the threading module either doesn't appear to run (and I get an unhandled exception in thread message in the console) or freezes up pythonista completely and I have to terminate it and restart it.

          So, rather than me posting a whole bunch of code which doesn't work...

          Can anyone point me to a threading sample which they know does work in pythonista? Has anyone had success with starting a simple worker thread and having it run in the background during a Scene-based pythonista program?

          I'm hoping someone has gotten this working and can point me in the right direction wrt how its 'supposed' to be done in pythonista.

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

            Sorry, I must have truncated the link somehow. Here is the correct link:

            http://omz-software.com/pythonista/forums/discussion/128/threading-module-crashes-the-app/p1

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

              I checked the link and I don't think it applies here. The problem noted there is that there's a crash when the main thread exits while worker threads are still running. This isn't the case here. I have a Scene based script which just continually runs.

              So, can anyone advise as to the correct way to successfully do threading in pythonista? Has anyone had success with this? I just want to start up a simple worker thread to do stuff in the background while my main Scene based script runs.

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

                Here's a simple demo: https://gist.github.com/gsong/6384563

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

                  I use a worker thread in my rssfeed example here. https://gist.github.com/GuyCarver/4173534
                  But my experience is that the main and worker threads do not schedule well. While the background thread is loading feeds the main thread may get choppy.

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

                    A few things you might want to consider:

                    1. Ending the main thread before all child threads have completed currently crashes the Pythonista app so consider using join() to keep your main thread alive.
                    2. You might want to try messing with os.nice() http://docs.python.org/2/library/os.html?highlight=os.nice#os.nice
                    3. You might try doing your work in the main thread and running your scene in a child thread. Perhaps there are performance differences between how much priority the main thread gets vs. the priority the child threads get.

                    This may or may not help.

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

                      @ccc Do you know of a way to detect when a Scene exits? It seems run(scene) itself runs in a thread.

                      Where would you add the join?

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

                        It is far stranger that I had thought...

                        Pythonista's Scene module only wants a single scene to run. Therefor the last Scene whose run() method is called (either in the main thread or a child thread) will be the ONLY Scene to execute. All others Scenes will ignored. That one Scene will not get launched (i.e. it's setup() method called) until AFTER the main thread has completed.

                        sceneAndThread.py. https://gist.github.com/cclauss/6399470

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

                          Check this...Python threading basics

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