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
-
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?
-
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:
http://omz-software.com/pythonista/forums/discussion/128/threading-module-crashes-the-app/p1
-
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.
-
Check this...Python threading basics