MCPcopy Index your code
hub / github.com/ipython/ipython / inputhook

Function inputhook

IPython/terminal/pt_inputhooks/glut.py:94–140  ·  view source on GitHub ↗

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

(context)

Source from the content-addressed store, hash-verified

92
93
94def inputhook(context):
95 """Run the pyglet event loop by processing pending events only.
96
97 This keeps processing pending events until stdin is ready. After
98 processing all pending events, a call to time.sleep is inserted. This is
99 needed, otherwise, CPU usage is at 100%. This sleep time should be tuned
100 though for best performance.
101 """
102 # We need to protect against a user pressing Control-C when IPython is
103 # idle and this is running. We trap KeyboardInterrupt and pass.
104
105 signal.signal(signal.SIGINT, glut_int_handler)
106
107 try:
108 t = clock()
109
110 # Make sure the default window is set after a window has been closed
111 if glut.glutGetWindow() == 0:
112 glut.glutSetWindow( 1 )
113 glutMainLoopEvent()
114 return 0
115
116 while not context.input_is_ready():
117 glutMainLoopEvent()
118 # We need to sleep at this point to keep the idle CPU load
119 # low. However, if sleep to long, GUI response is poor. As
120 # a compromise, we watch how often GUI events are being processed
121 # and switch between a short and long sleep time. Here are some
122 # stats useful in helping to tune this.
123 # time CPU load
124 # 0.001 13%
125 # 0.005 3%
126 # 0.01 1.5%
127 # 0.05 0.5%
128 used_time = clock() - t
129 if used_time > 10.0:
130 # print('Sleep for 1 s') # dbg
131 time.sleep(1.0)
132 elif used_time > 0.1:
133 # Few GUI events coming in, so we can sleep longer
134 # print('Sleep for 0.05 s') # dbg
135 time.sleep(0.05)
136 else:
137 # Many GUI events coming in, so sleep only very little
138 time.sleep(0.001)
139 except KeyboardInterrupt:
140 pass

Callers

nothing calls this directly

Calls 1

clockFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…