Bug 7809 - Python widgets can't use threads
: Python widgets can't use threads
Status: RESOLVED WONTFIX
Product: Desktop platform
Home
: 5.0/(1.2009.42-11)
: All Maemo
: Low normal (vote)
: ---
Assigned To: unassigned
: home-bugs
:
: moreinfo
:
:
  Show dependency tree
 
Reported: 2010-01-11 18:00 UTC by Alexey Guseynov
Modified: 2010-01-22 20:49 UTC (History)
3 users (show)

See Also:


Attachments


Note

You need to log in before you can comment on or make changes to this bug.


Description Alexey Guseynov (reporter) 2010-01-11 18:00:31 UTC
SOFTWARE VERSION:
5.0/1.2009.42.-1

EXACT STEPS LEADING TO PROBLEM: 
Python developers had taken care about low qualified programmers and
implemented their python threads in a non standard manier. They decided, that
making explicit synchronization is good, but that doesn't work with GTK.

I'm writing a widget, which needs about 15 seconds to react to a user because
of network delays. Making query in main thread make hildon-home to hung for
these 15 seconds. The solution is obvilous: use threads.

1) Make a GTK python application (In my case the application is hildon-home, so
that's why I'm writing here).
2) Create a thread (for example from a widget).

EXPECTED OUTCOME:

Thread should be run in parallel

ACTUAL OUTCOME:

Thread is suspended as soon, as function, from which it was created finishes.
It may be resumed later, for example if I run time.sleep() from main thread.
But as far, as I don't need to run sleep, it would be suspended forever.

REPRODUCIBILITY:
always

OTHER COMMENTS:

The solution is easy: run gtk.gdk.threads_init()
http://www.moeraki.com/pygtktutorial/pygtk2reference/gdk-functions.html#function-gdk--threads-init

This will fix deadlocking of GTK and threads and widget would run as intended.
But this function MUST be run before gtk.main(), so I can't run it from my
widget. So please either add invocation of gtk.gdk.threads_init() to
hildon-home to allow python widgets make threads and make interface more
responsive or add to somewhere to documentation that python widgets can't make
long calculations due to bad design of python threads.

P.S. Right now I think I should reimplement my widget in C++.
Comment 1 Martin Grimme 2010-01-11 19:47:40 UTC
Other solution: don't use threads (they are badly broken in Python running with
PyGTK anyway), but use gobject IO callbacks.
Comment 2 Andre Klapper maemo.org 2010-01-11 22:06:59 UTC
So why is this a Maemo Desktop platform issue and not filed under PyGtk?

And what is the exact request to change here?
Comment 3 Alexey Guseynov (reporter) 2010-01-11 22:29:34 UTC
(In reply to comment #2)
> So why is this a Maemo Desktop platform issue and not filed under PyGtk?
> 
> And what is the exact request to change here?
> 

This bug is filled here because it can be fixed only in hildon-home or you can
decide not to fix it at all.

Design of python (so it is won't fix there) is so, that threads in widgets
would only work if hildon-home before calling gtk.main() would call
gtk.gdk.threads_init(). I don't know if it is possible.

If you find, that such fix is not suitable because it's ugly, then this issue
should be addressed to PyGTK with request to add to documentation warning, that
threads are not supported for widgets by design and developers should consider
using other languages for developing widgets, which need parallelism.
Comment 4 Alexey Guseynov (reporter) 2010-01-22 20:49:54 UTC
(In reply to comment #1)
> Other solution: don't use threads (they are badly broken in Python running with
> PyGTK anyway), but use gobject IO callbacks.
> 

I agree. IO callbacks are better solution in this case and can be used to write
any parallel code (with some loss of efficiency). But if you want speed python
(as any script language) is not the language you want to use.