correct way to do threading in pythonista
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).
someVar += 1
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?
For the crash, maybe it's the problem described here:
For the other problem, can you post a complete example?
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.
Sorry, I must have truncated the link somehow. Here is the correct link:
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.
Here's a simple demo: https://gist.github.com/gsong/6384563
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.
A few things you might want to consider:
- 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.
- You might want to try messing with os.nice() http://docs.python.org/2/library/os.html?highlight=os.nice#os.nice
- 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.
@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?
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.