Show More
@@ -8,30 +8,45 b' from timeit import default_timer as clock' | |||||
8 | import wx |
|
8 | import wx | |
9 |
|
9 | |||
10 |
|
10 | |||
|
11 | def ignore_keyboardinterrupts(func): | |||
|
12 | """Decorator which causes KeyboardInterrupt exceptions to be ignored during | |||
|
13 | execution of the decorated function. | |||
|
14 | ||||
|
15 | This is used by the inputhook functions to handle the event where the user | |||
|
16 | presses CTRL+C while IPython is idle, and the inputhook loop is running. In | |||
|
17 | this case, we want to ignore interrupts. | |||
|
18 | """ | |||
|
19 | def wrapper(*args, **kwargs): | |||
|
20 | try: | |||
|
21 | func(*args, **kwargs) | |||
|
22 | except KeyboardInterrupt: | |||
|
23 | pass | |||
|
24 | return wrapper | |||
|
25 | ||||
|
26 | ||||
|
27 | @ignore_keyboardinterrupts | |||
11 | def inputhook_wx1(context): |
|
28 | def inputhook_wx1(context): | |
12 | """Run the wx event loop by processing pending events only. |
|
29 | """Run the wx event loop by processing pending events only. | |
13 |
|
30 | |||
14 | This approach seems to work, but its performance is not great as it |
|
31 | This approach seems to work, but its performance is not great as it | |
15 | relies on having PyOS_InputHook called regularly. |
|
32 | relies on having PyOS_InputHook called regularly. | |
16 | """ |
|
33 | """ | |
17 | try: |
|
34 | app = wx.GetApp() | |
18 | app = wx.GetApp() |
|
35 | if app is not None: | |
19 | if app is not None: |
|
36 | assert wx.Thread_IsMain() | |
20 | assert wx.Thread_IsMain() |
|
37 | ||
21 |
|
38 | # Make a temporary event loop and process system events until | ||
22 | # Make a temporary event loop and process system events until |
|
39 | # there are no more waiting, then allow idle events (which | |
23 | # there are no more waiting, then allow idle events (which |
|
40 | # will also deal with pending or posted wx events.) | |
24 | # will also deal with pending or posted wx events.) |
|
41 | evtloop = wx.EventLoop() | |
25 |
|
|
42 | ea = wx.EventLoopActivator(evtloop) | |
26 | ea = wx.EventLoopActivator(evtloop) |
|
43 | while evtloop.Pending(): | |
27 |
|
|
44 | evtloop.Dispatch() | |
28 | evtloop.Dispatch() |
|
45 | app.ProcessIdle() | |
29 | app.ProcessIdle() |
|
46 | del ea | |
30 | del ea |
|
|||
31 | except KeyboardInterrupt: |
|
|||
32 | pass |
|
|||
33 | return 0 |
|
47 | return 0 | |
34 |
|
48 | |||
|
49 | ||||
35 | class EventLoopTimer(wx.Timer): |
|
50 | class EventLoopTimer(wx.Timer): | |
36 |
|
51 | |||
37 | def __init__(self, func): |
|
52 | def __init__(self, func): | |
@@ -41,6 +56,7 b' class EventLoopTimer(wx.Timer):' | |||||
41 | def Notify(self): |
|
56 | def Notify(self): | |
42 | self.func() |
|
57 | self.func() | |
43 |
|
58 | |||
|
59 | ||||
44 | class EventLoopRunner(object): |
|
60 | class EventLoopRunner(object): | |
45 |
|
61 | |||
46 | def Run(self, time, input_is_ready): |
|
62 | def Run(self, time, input_is_ready): | |
@@ -55,6 +71,8 b' class EventLoopRunner(object):' | |||||
55 | self.timer.Stop() |
|
71 | self.timer.Stop() | |
56 | self.evtloop.Exit() |
|
72 | self.evtloop.Exit() | |
57 |
|
73 | |||
|
74 | ||||
|
75 | @ignore_keyboardinterrupts | |||
58 | def inputhook_wx2(context): |
|
76 | def inputhook_wx2(context): | |
59 | """Run the wx event loop, polling for stdin. |
|
77 | """Run the wx event loop, polling for stdin. | |
60 |
|
78 | |||
@@ -69,19 +87,18 b' def inputhook_wx2(context):' | |||||
69 | but eventually performance would suffer from calling select/kbhit too |
|
87 | but eventually performance would suffer from calling select/kbhit too | |
70 | often. |
|
88 | often. | |
71 | """ |
|
89 | """ | |
72 | try: |
|
90 | app = wx.GetApp() | |
73 | app = wx.GetApp() |
|
91 | if app is not None: | |
74 | if app is not None: |
|
92 | assert wx.Thread_IsMain() | |
75 | assert wx.Thread_IsMain() |
|
93 | elr = EventLoopRunner() | |
76 | elr = EventLoopRunner() |
|
94 | # As this time is made shorter, keyboard response improves, but idle | |
77 | # As this time is made shorter, keyboard response improves, but idle |
|
95 | # CPU load goes up. 10 ms seems like a good compromise. | |
78 | # CPU load goes up. 10 ms seems like a good compromise. |
|
96 | elr.Run(time=10, # CHANGE time here to control polling interval | |
79 | elr.Run(time=10, # CHANGE time here to control polling interval |
|
97 | input_is_ready=context.input_is_ready) | |
80 | input_is_ready=context.input_is_ready) |
|
|||
81 | except KeyboardInterrupt: |
|
|||
82 | pass |
|
|||
83 | return 0 |
|
98 | return 0 | |
84 |
|
99 | |||
|
100 | ||||
|
101 | @ignore_keyboardinterrupts | |||
85 | def inputhook_wx3(context): |
|
102 | def inputhook_wx3(context): | |
86 | """Run the wx event loop by processing pending events only. |
|
103 | """Run the wx event loop by processing pending events only. | |
87 |
|
104 | |||
@@ -90,58 +107,111 b' def inputhook_wx3(context):' | |||||
90 | time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. |
|
107 | time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. | |
91 | This sleep time should be tuned though for best performance. |
|
108 | This sleep time should be tuned though for best performance. | |
92 | """ |
|
109 | """ | |
93 | # We need to protect against a user pressing Control-C when IPython is |
|
110 | app = wx.GetApp() | |
94 | # idle and this is running. We trap KeyboardInterrupt and pass. |
|
111 | if app is not None: | |
95 | try: |
|
112 | assert wx.Thread_IsMain() | |
96 | app = wx.GetApp() |
|
113 | ||
97 | if app is not None: |
|
114 | # The import of wx on Linux sets the handler for signal.SIGINT | |
98 | assert wx.Thread_IsMain() |
|
115 | # to 0. This is a bug in wx or gtk. We fix by just setting it | |
99 |
|
116 | # back to the Python default. | ||
100 | # The import of wx on Linux sets the handler for signal.SIGINT |
|
117 | if not callable(signal.getsignal(signal.SIGINT)): | |
101 | # to 0. This is a bug in wx or gtk. We fix by just setting it |
|
118 | signal.signal(signal.SIGINT, signal.default_int_handler) | |
102 | # back to the Python default. |
|
119 | ||
103 | if not callable(signal.getsignal(signal.SIGINT)): |
|
120 | evtloop = wx.EventLoop() | |
104 | signal.signal(signal.SIGINT, signal.default_int_handler) |
|
121 | ea = wx.EventLoopActivator(evtloop) | |
105 |
|
122 | t = clock() | ||
106 | evtloop = wx.EventLoop() |
|
123 | while not context.input_is_ready(): | |
107 | ea = wx.EventLoopActivator(evtloop) |
|
124 | while evtloop.Pending(): | |
108 | t = clock() |
|
125 | t = clock() | |
109 | while not context.input_is_ready(): |
|
126 | evtloop.Dispatch() | |
110 | while evtloop.Pending(): |
|
127 | app.ProcessIdle() | |
111 | t = clock() |
|
128 | # We need to sleep at this point to keep the idle CPU load | |
112 | evtloop.Dispatch() |
|
129 | # low. However, if sleep to long, GUI response is poor. As | |
113 | app.ProcessIdle() |
|
130 | # a compromise, we watch how often GUI events are being processed | |
114 | # We need to sleep at this point to keep the idle CPU load |
|
131 | # and switch between a short and long sleep time. Here are some | |
115 | # low. However, if sleep to long, GUI response is poor. As |
|
132 | # stats useful in helping to tune this. | |
116 | # a compromise, we watch how often GUI events are being processed |
|
133 | # time CPU load | |
117 | # and switch between a short and long sleep time. Here are some |
|
134 | # 0.001 13% | |
118 | # stats useful in helping to tune this. |
|
135 | # 0.005 3% | |
119 |
|
|
136 | # 0.01 1.5% | |
120 |
|
|
137 | # 0.05 0.5% | |
121 | # 0.005 3% |
|
138 | used_time = clock() - t | |
122 | # 0.01 1.5% |
|
139 | if used_time > 10.0: | |
123 | # 0.05 0.5% |
|
140 | # print 'Sleep for 1 s' # dbg | |
124 |
|
|
141 | time.sleep(1.0) | |
125 |
|
|
142 | elif used_time > 0.1: | |
126 | # print 'Sleep for 1 s' # dbg |
|
143 | # Few GUI events coming in, so we can sleep longer | |
127 | time.sleep(1.0) |
|
144 | # print 'Sleep for 0.05 s' # dbg | |
128 |
|
|
145 | time.sleep(0.05) | |
129 | # Few GUI events coming in, so we can sleep longer |
|
146 | else: | |
130 | # print 'Sleep for 0.05 s' # dbg |
|
147 | # Many GUI events coming in, so sleep only very little | |
131 |
|
|
148 | time.sleep(0.001) | |
132 | else: |
|
149 | del ea | |
133 | # Many GUI events coming in, so sleep only very little |
|
|||
134 | time.sleep(0.001) |
|
|||
135 | del ea |
|
|||
136 | except KeyboardInterrupt: |
|
|||
137 | pass |
|
|||
138 | return 0 |
|
150 | return 0 | |
139 |
|
151 | |||
140 | if sys.platform == 'darwin': |
|
152 | ||
141 | # On OSX, evtloop.Pending() always returns True, regardless of there being |
|
153 | @ignore_keyboardinterrupts | |
142 | # any events pending. As such we can't use implementations 1 or 3 of the |
|
154 | def inputhook_wxphoenix(context): | |
143 | # inputhook as those depend on a pending/dispatch loop. |
|
155 | """Run the wx event loop until the user provides more input. | |
|
156 | ||||
|
157 | This input hook is suitable for use with wxPython >= 4 (a.k.a. Phoenix). | |||
|
158 | ||||
|
159 | It uses the same approach to that used in | |||
|
160 | ipykernel.eventloops.loop_wx. The wx.MainLoop is executed, and a wx.Timer | |||
|
161 | is used to periodically poll the context for input. As soon as input is | |||
|
162 | ready, the wx.MainLoop is stopped. | |||
|
163 | """ | |||
|
164 | ||||
|
165 | app = wx.GetApp() | |||
|
166 | ||||
|
167 | if app is None: | |||
|
168 | return | |||
|
169 | ||||
|
170 | if context.input_is_ready(): | |||
|
171 | return | |||
|
172 | ||||
|
173 | assert wx.IsMainThread() | |||
|
174 | ||||
|
175 | # Wx uses milliseconds | |||
|
176 | poll_interval = 100 | |||
|
177 | ||||
|
178 | # Use a wx.Timer to periodically check whether input is ready - as soon as | |||
|
179 | # it is, we exit the main loop | |||
|
180 | def poll(ev): | |||
|
181 | if context.input_is_ready(): | |||
|
182 | app.ExitMainLoop() | |||
|
183 | ||||
|
184 | timer = wx.Timer() | |||
|
185 | timer.Start(poll_interval) | |||
|
186 | timer.Bind(wx.EVT_TIMER, poll) | |||
|
187 | ||||
|
188 | # The import of wx on Linux sets the handler for signal.SIGINT to 0. This | |||
|
189 | # is a bug in wx or gtk. We fix by just setting it back to the Python | |||
|
190 | # default. | |||
|
191 | if not callable(signal.getsignal(signal.SIGINT)): | |||
|
192 | signal.signal(signal.SIGINT, signal.default_int_handler) | |||
|
193 | ||||
|
194 | # The SetExitOnFrameDelete call allows us to run the wx mainloop without | |||
|
195 | # having a frame open. | |||
|
196 | app.SetExitOnFrameDelete(False) | |||
|
197 | app.MainLoop() | |||
|
198 | ||||
|
199 | ||||
|
200 | # Get the major wx version number to figure out what input hook we should use. | |||
|
201 | major_version = 3 | |||
|
202 | ||||
|
203 | try: | |||
|
204 | major_version = int(wx.__version__[0]) | |||
|
205 | except Exception: | |||
|
206 | pass | |||
|
207 | ||||
|
208 | # Use the phoenix hook on all platforms for wxpython >= 4 | |||
|
209 | if major_version >= 4: | |||
|
210 | inputhook = inputhook_wxphoenix | |||
|
211 | # On OSX, evtloop.Pending() always returns True, regardless of there being | |||
|
212 | # any events pending. As such we can't use implementations 1 or 3 of the | |||
|
213 | # inputhook as those depend on a pending/dispatch loop. | |||
|
214 | elif sys.platform == 'darwin': | |||
144 | inputhook = inputhook_wx2 |
|
215 | inputhook = inputhook_wx2 | |
145 | else: |
|
216 | else: | |
146 | # This is our default implementation |
|
|||
147 | inputhook = inputhook_wx3 |
|
217 | inputhook = inputhook_wx3 |
General Comments 0
You need to be logged in to leave comments.
Login now