1
2 import gtk
3 import time
4 import gobject
5 from threading import currentThread
6
7 from Queue import Queue
8
9 gtk.gdk.threads_init()
12 '''
13 A decorator which causes the function to be called by the gtk
14 main iteration loop rather than synchronously...
15
16 NOTE: This makes the call async handled by the gtk main
17 loop code. you can NOT return anything.
18 '''
19 def dowork(arginfo):
20 args,kwargs = arginfo
21 return func(*args, **kwargs)
22
23 def idleadd(*args, **kwargs):
24 if currentThread().getName() == 'GtkThread':
25 return func(*args, **kwargs)
26 gtk.gdk.threads_enter()
27 gobject.idle_add(dowork, (args,kwargs))
28 gtk.gdk.threads_leave()
29
30 return idleadd
31
33 '''
34 Similar to idlethread except that it is synchronous and able
35 to return values.
36 '''
37 q = Queue()
38 def dowork(arginfo):
39 args,kwargs = arginfo
40 try:
41 q.put(func(*args, **kwargs))
42 except Exception, e:
43 q.put(e)
44
45 def idleadd(*args, **kwargs):
46 if currentThread().getName() == 'GtkThread':
47 return func(*args, **kwargs)
48 gtk.gdk.threads_enter()
49 gobject.idle_add(dowork, (args,kwargs))
50 gtk.gdk.threads_leave()
51 return q.get()
52
53 return idleadd
54
58
60 currentThread().setName('GtkThread')
61
63 currentThread().setName('GtkThread')
64 gtk.gdk.threads_enter()
65 gtk.main()
66 gtk.gdk.threads_leave()
67
69 gtk.gdk.threads_enter()
70 while gtk.events_pending():
71 gtk.main_iteration(False)
72 gtk.gdk.threads_leave()
73