Development Team/Almanac: Difference between revisions
Add link to API changes |
|||
| Line 202: | Line 202: | ||
=== My Python activity wants to use threads; how do I do that? === | === My Python activity wants to use threads; how do I do that? === | ||
A question that has been answered with limited success is which threading patterns are most appropriate for use in Sugar. The following pattern of code to work fine in basic instances: | |||
<pre> | |||
#### Method: __init__, initialize this AnnotateActivity instance | |||
def __init__(self, handle): | |||
... | |||
self.sample_thread = Thread(target=self.announce_thread, args=()) | |||
self.sample_thread.setDaemon(0) | |||
self.sample_thread.start() | |||
... | |||
def announce_thread(self): | |||
while (self.Running): | |||
time.sleep(1) | |||
print "thread running" | |||
self._update_chat_text("Thread", "In here") | |||
</pre> | |||
This is the basic series of steps that most online documentation on python suggests to use when trying to work with threads in python. The problem is that it is unclear how this pattern relates to code that worked in the SimCity activity: | |||
<pre> | |||
import gobject | |||
gobject.threads_init() | |||
#import dbus.mainloop.glib | |||
#dbus.mainloop.glib.threads_init() | |||
</pre> | |||
It should be noted that in the SimCity activity the pygame sound player would not produce sound reliably unless this setup was done. | |||
<p/> | |||
Should the two patterns always be used in tandem? It seems that the latter code is mainly to initiate gobject and other libraries to work with threading, but it is unclear what restrictions there are with using threading with these libraries. Does one take precedence over the other? It is not clear if there is any problem with using the standard python threading code on the sugar technology stack. | |||
<p/> | |||
In fact, experiments with threading on sugar leads to several different problems. For one thing, thread termination was tricky - using the can_close() method for sugar activities to terminate an activity only killed threads in some circumstances. It did not properly handle terminating threads in the case of CTRL-C or terminal interrupts. You can try to catch signals (SIGINT, SIGTERM or SIGHUP), but you will still be running in to errors in terminating child threads using these as well. | |||
<p/> | |||
Another set of errors with threading comes up when trying to combine with stream tubes. The bottom line is that it is unclear what the scope of threading in a Sugar activity should be - should it simply work if you do the standard python threading pattern, is the use of the glib.threads_init and gobject.threads_init calls necessary, are there other interactions with threads and dbus that need to be accounted for? With more clarity from sugar developers on how the platform envisions threading to work in an activity, we can be more comfortable writing entries in the Almanac to help developers write error-free code. | |||
=== How do I customize the title that is displayed for each instance of my activity? === | === How do I customize the title that is displayed for each instance of my activity? === | ||