1
2
3
4
5 """ Glib2/gobject based reactor. Works trasparently with gobject-dependent
6 things, such as Dbus.
7 """
8
9 from brisa.core.ireactor import ReactorInterface, EVENT_TYPE_READ, \
10 EVENT_TYPE_WRITE, EVENT_TYPE_EXCEPTION
11
12 import signal
13
14 try:
15 import gobject
16 __all__ = ('GLib2Reactor', )
17 except ImportError:
18 __all__ = ()
19
20
22
23 _stop_funcs = []
24 _start_funcs = []
25 _main_loop = gobject.MainLoop()
26
31
32 - def add_timer(self, interval, callback, threshold=0):
33 """ Add timer.
34
35 @note: should return an ID assigned to the timer, so that it can be
36 removed by rem_timer().
37 """
38 return gobject.timeout_add(int(interval*(10**3)), callback)
39
41 """ Removes a timer.
42 """
43 return gobject.source_remove(timer_id)
44
45 - def add_fd(self, fd, event_callback, event_type):
46 """ Adds a fd for watch.
47 """
48 condition = None
49 if event_type & EVENT_TYPE_READ:
50 condition = gobject.IO_IN | gobject.IO_PRI
51 if event_type & EVENT_TYPE_WRITE:
52 if not condition:
53 condition = gobject.IO_OUT
54 else:
55 condition = condition | gobject.IO_OUT
56 if event_type & EVENT_TYPE_EXCEPTION:
57 if not condition:
58 condition = gobject.IO_ERR
59 else:
60 condition = condition | gobject.IO_ERR
61
62 return gobject.io_add_watch(fd, condition, event_callback)
63
65 """ Removes a fd from being watched.
66 """
67 return gobject.source_remove(fd_handler)
68
70 """ Registers a function to be called before entering the STOPPED
71 state.
72
73 @param func: function
74 @type func: callable
75 """
76 if func not in self._stop_funcs:
77 self._stop_funcs.append(func)
78
80 """ Removes a registered function.
81
82 @param func: function
83 @type func: callable
84 """
85 if func in self._stop_funcs:
86 self._stop_funcs.remove(func)
87
89 """ Registers a function to be called before starting the main loop.
90
91 @param func: function
92 @type func: callable
93 """
94 if func not in self._start_funcs:
95 self._start_funcs.append(func)
96
98 """ Removes a registered function.
99
100 @param func: function
101 @type func: callable
102 """
103 if func in self._start_funcs:
104 self._start_funcs.remove(func)
105
107 """ Runs a single iteration of the main loop. Reactor enters the
108 RUNNING state while this method executes.
109 """
110 self._main_loop.get_context().iteration()
111
113 """ Enters the RUNNING state by running the main loop until
114 main_quit() is called.
115 """
116 self._main_call_before_start_funcs()
117 try:
118 self._main_loop.run()
119 except KeyboardInterrupt:
120 pass
121 self._main_call_after_stop_funcs()
122
123 - def main_quit(self):
124 """ Terminates the main loop.
125 """
126 self._main_loop.quit()
127
129 """ Returns True if the main loop is running
130 """
131 return self._main_loop.get_context().is_running()
132
134 """ Calls registered functions to be called after the main loop is
135 stopped.
136 """
137 for cb in self._stop_funcs:
138 cb()
139
141 """ Call registered functions to be called before starting the main
142 loop.
143 """
144 for cb in self._start_funcs:
145 cb()
146