Show More
@@ -8,13 +8,25 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 | |||
|
13 | ignored during execution of the decorated function. | |||
|
14 | """ | |||
|
15 | def wrapper(*args, **kwargs): | |||
|
16 | try: | |||
|
17 | func(*args, **kwargs) | |||
|
18 | except KeyboardInterrupt: | |||
|
19 | pass | |||
|
20 | return wrapper | |||
|
21 | ||||
|
22 | ||||
|
23 | @ignore_keyboardinterrupts | |||
11 | def inputhook_wx1(context): |
|
24 | def inputhook_wx1(context): | |
12 | """Run the wx event loop by processing pending events only. |
|
25 | """Run the wx event loop by processing pending events only. | |
13 |
|
26 | |||
14 | This approach seems to work, but its performance is not great as it |
|
27 | This approach seems to work, but its performance is not great as it | |
15 | relies on having PyOS_InputHook called regularly. |
|
28 | relies on having PyOS_InputHook called regularly. | |
16 | """ |
|
29 | """ | |
17 | try: |
|
|||
18 |
|
|
30 | app = wx.GetApp() | |
19 |
|
|
31 | if app is not None: | |
20 |
|
|
32 | assert wx.Thread_IsMain() | |
@@ -28,10 +40,9 b' def inputhook_wx1(context):' | |||||
28 |
|
|
40 | evtloop.Dispatch() | |
29 |
|
|
41 | app.ProcessIdle() | |
30 |
|
|
42 | del ea | |
31 | except KeyboardInterrupt: |
|
|||
32 | pass |
|
|||
33 | return 0 |
|
43 | return 0 | |
34 |
|
44 | |||
|
45 | ||||
35 | class EventLoopTimer(wx.Timer): |
|
46 | class EventLoopTimer(wx.Timer): | |
36 |
|
47 | |||
37 | def __init__(self, func): |
|
48 | def __init__(self, func): | |
@@ -41,6 +52,7 b' class EventLoopTimer(wx.Timer):' | |||||
41 | def Notify(self): |
|
52 | def Notify(self): | |
42 | self.func() |
|
53 | self.func() | |
43 |
|
54 | |||
|
55 | ||||
44 | class EventLoopRunner(object): |
|
56 | class EventLoopRunner(object): | |
45 |
|
57 | |||
46 | def Run(self, time, input_is_ready): |
|
58 | def Run(self, time, input_is_ready): | |
@@ -55,6 +67,8 b' class EventLoopRunner(object):' | |||||
55 | self.timer.Stop() |
|
67 | self.timer.Stop() | |
56 | self.evtloop.Exit() |
|
68 | self.evtloop.Exit() | |
57 |
|
69 | |||
|
70 | ||||
|
71 | @ignore_keyboardinterrupts | |||
58 | def inputhook_wx2(context): |
|
72 | def inputhook_wx2(context): | |
59 | """Run the wx event loop, polling for stdin. |
|
73 | """Run the wx event loop, polling for stdin. | |
60 |
|
74 | |||
@@ -69,7 +83,6 b' def inputhook_wx2(context):' | |||||
69 | but eventually performance would suffer from calling select/kbhit too |
|
83 | but eventually performance would suffer from calling select/kbhit too | |
70 | often. |
|
84 | often. | |
71 | """ |
|
85 | """ | |
72 | try: |
|
|||
73 |
|
|
86 | app = wx.GetApp() | |
74 |
|
|
87 | if app is not None: | |
75 |
|
|
88 | assert wx.Thread_IsMain() | |
@@ -78,10 +91,10 b' def inputhook_wx2(context):' | |||||
78 |
|
|
91 | # CPU load goes up. 10 ms seems like a good compromise. | |
79 |
|
|
92 | elr.Run(time=10, # CHANGE time here to control polling interval | |
80 |
|
|
93 | input_is_ready=context.input_is_ready) | |
81 | except KeyboardInterrupt: |
|
|||
82 | pass |
|
|||
83 | return 0 |
|
94 | return 0 | |
84 |
|
95 | |||
|
96 | ||||
|
97 | @ignore_keyboardinterrupts | |||
85 | def inputhook_wx3(context): |
|
98 | def inputhook_wx3(context): | |
86 | """Run the wx event loop by processing pending events only. |
|
99 | """Run the wx event loop by processing pending events only. | |
87 |
|
100 | |||
@@ -90,9 +103,6 b' def inputhook_wx3(context):' | |||||
90 | time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. |
|
103 | time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. | |
91 | This sleep time should be tuned though for best performance. |
|
104 | This sleep time should be tuned though for best performance. | |
92 | """ |
|
105 | """ | |
93 | # We need to protect against a user pressing Control-C when IPython is |
|
|||
94 | # idle and this is running. We trap KeyboardInterrupt and pass. |
|
|||
95 | try: |
|
|||
96 |
|
|
106 | app = wx.GetApp() | |
97 |
|
|
107 | if app is not None: | |
98 |
|
|
108 | assert wx.Thread_IsMain() | |
@@ -133,11 +143,10 b' def inputhook_wx3(context):' | |||||
133 |
|
|
143 | # Many GUI events coming in, so sleep only very little | |
134 |
|
|
144 | time.sleep(0.001) | |
135 |
|
|
145 | del ea | |
136 | except KeyboardInterrupt: |
|
|||
137 | pass |
|
|||
138 | return 0 |
|
146 | return 0 | |
139 |
|
147 | |||
140 |
|
148 | |||
|
149 | @ignore_keyboardinterrupts | |||
141 | def inputhook_wxphoenix(context): |
|
150 | def inputhook_wxphoenix(context): | |
142 | """Run the wx event loop until the user provides more input. |
|
151 | """Run the wx event loop until the user provides more input. | |
143 |
|
152 | |||
@@ -183,6 +192,7 b' def inputhook_wxphoenix(context):' | |||||
183 |
|
192 | |||
184 | app.MainLoop() |
|
193 | app.MainLoop() | |
185 |
|
194 | |||
|
195 | ||||
186 | # Get the major wx version number to figure out what input hook we should use. |
|
196 | # Get the major wx version number to figure out what input hook we should use. | |
187 | major_version = 3 |
|
197 | major_version = 3 | |
188 | try: |
|
198 | try: |
General Comments 0
You need to be logged in to leave comments.
Login now