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