Run the wx event loop by processing pending events only. This is like inputhook_wx1, but it keeps processing pending events until stdin is ready. After processing all pending events, a call to time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. This sleep time
()
| 103 | return 0 |
| 104 | |
| 105 | def inputhook_wx3(): |
| 106 | """Run the wx event loop by processing pending events only. |
| 107 | |
| 108 | This is like inputhook_wx1, but it keeps processing pending events |
| 109 | until stdin is ready. After processing all pending events, a call to |
| 110 | time.sleep is inserted. This is needed, otherwise, CPU usage is at 100%. |
| 111 | This sleep time should be tuned though for best performance. |
| 112 | """ |
| 113 | # We need to protect against a user pressing Control-C when IPython is |
| 114 | # idle and this is running. We trap KeyboardInterrupt and pass. |
| 115 | try: |
| 116 | app = wx.GetApp() |
| 117 | if app is not None: |
| 118 | assert wx.Thread_IsMain() |
| 119 | |
| 120 | # The import of wx on Linux sets the handler for signal.SIGINT |
| 121 | # to 0. This is a bug in wx or gtk. We fix by just setting it |
| 122 | # back to the Python default. |
| 123 | if not callable(signal.getsignal(signal.SIGINT)): |
| 124 | signal.signal(signal.SIGINT, signal.default_int_handler) |
| 125 | |
| 126 | evtloop = wx.EventLoop() |
| 127 | ea = wx.EventLoopActivator(evtloop) |
| 128 | t = clock() |
| 129 | while not stdin_ready(): |
| 130 | while evtloop.Pending(): |
| 131 | t = clock() |
| 132 | evtloop.Dispatch() |
| 133 | app.ProcessIdle() |
| 134 | # We need to sleep at this point to keep the idle CPU load |
| 135 | # low. However, if sleep to long, GUI response is poor. As |
| 136 | # a compromise, we watch how often GUI events are being processed |
| 137 | # and switch between a short and long sleep time. Here are some |
| 138 | # stats useful in helping to tune this. |
| 139 | # time CPU load |
| 140 | # 0.001 13% |
| 141 | # 0.005 3% |
| 142 | # 0.01 1.5% |
| 143 | # 0.05 0.5% |
| 144 | used_time = clock() - t |
| 145 | if used_time > 10.0: |
| 146 | # print 'Sleep for 1 s' # dbg |
| 147 | time.sleep(1.0) |
| 148 | elif used_time > 0.1: |
| 149 | # Few GUI events coming in, so we can sleep longer |
| 150 | # print 'Sleep for 0.05 s' # dbg |
| 151 | time.sleep(0.05) |
| 152 | else: |
| 153 | # Many GUI events coming in, so sleep only very little |
| 154 | time.sleep(0.001) |
| 155 | del ea |
| 156 | except KeyboardInterrupt: |
| 157 | pass |
| 158 | return 0 |
| 159 | |
| 160 | if sys.platform == 'darwin': |
| 161 | # On OSX, evtloop.Pending() always returns True, regardless of there being |
nothing calls this directly
no test coverage detected