Run the pyglet event loop by processing pending events only. This 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 should be tuned though f
()
| 123 | # Code |
| 124 | #----------------------------------------------------------------------------- |
| 125 | def inputhook_glut(): |
| 126 | """Run the pyglet event loop by processing pending events only. |
| 127 | |
| 128 | This keeps processing pending events until stdin is ready. After |
| 129 | processing all pending events, a call to time.sleep is inserted. This is |
| 130 | needed, otherwise, CPU usage is at 100%. This sleep time should be tuned |
| 131 | though for best performance. |
| 132 | """ |
| 133 | # We need to protect against a user pressing Control-C when IPython is |
| 134 | # idle and this is running. We trap KeyboardInterrupt and pass. |
| 135 | |
| 136 | signal.signal(signal.SIGINT, glut_int_handler) |
| 137 | |
| 138 | try: |
| 139 | t = clock() |
| 140 | |
| 141 | # Make sure the default window is set after a window has been closed |
| 142 | if glut.glutGetWindow() == 0: |
| 143 | glut.glutSetWindow( 1 ) |
| 144 | glutMainLoopEvent() |
| 145 | return 0 |
| 146 | |
| 147 | while not stdin_ready(): |
| 148 | glutMainLoopEvent() |
| 149 | # We need to sleep at this point to keep the idle CPU load |
| 150 | # low. However, if sleep to long, GUI response is poor. As |
| 151 | # a compromise, we watch how often GUI events are being processed |
| 152 | # and switch between a short and long sleep time. Here are some |
| 153 | # stats useful in helping to tune this. |
| 154 | # time CPU load |
| 155 | # 0.001 13% |
| 156 | # 0.005 3% |
| 157 | # 0.01 1.5% |
| 158 | # 0.05 0.5% |
| 159 | used_time = clock() - t |
| 160 | if used_time > 10.0: |
| 161 | # print 'Sleep for 1 s' # dbg |
| 162 | time.sleep(1.0) |
| 163 | elif used_time > 0.1: |
| 164 | # Few GUI events coming in, so we can sleep longer |
| 165 | # print 'Sleep for 0.05 s' # dbg |
| 166 | time.sleep(0.05) |
| 167 | else: |
| 168 | # Many GUI events coming in, so sleep only very little |
| 169 | time.sleep(0.001) |
| 170 | except KeyboardInterrupt: |
| 171 | pass |
| 172 | return 0 |
nothing calls this directly
no test coverage detected