MCPcopy Create free account
hub / github.com/ipython/ipython / inputhook

Function inputhook

IPython/terminal/pt_inputhooks/pyglet.py:24–66  ·  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

22
23
24def inputhook(context):
25 """Run the pyglet event loop by processing pending events only.
26
27 This keeps processing pending events until stdin is ready. After
28 processing all pending events, a call to time.sleep is inserted. This is
29 needed, otherwise, CPU usage is at 100%. This sleep time should be tuned
30 though for best performance.
31 """
32 # We need to protect against a user pressing Control-C when IPython is
33 # idle and this is running. We trap KeyboardInterrupt and pass.
34 try:
35 t = clock()
36 while not context.input_is_ready():
37 pyglet.clock.tick()
38 for window in pyglet.app.windows:
39 window.switch_to()
40 window.dispatch_events()
41 window.dispatch_event('on_draw')
42 flip(window)
43
44 # We need to sleep at this point to keep the idle CPU load
45 # low. However, if sleep to long, GUI response is poor. As
46 # a compromise, we watch how often GUI events are being processed
47 # and switch between a short and long sleep time. Here are some
48 # stats useful in helping to tune this.
49 # time CPU load
50 # 0.001 13%
51 # 0.005 3%
52 # 0.01 1.5%
53 # 0.05 0.5%
54 used_time = clock() - t
55 if used_time > 10.0:
56 # print 'Sleep for 1 s' # dbg
57 time.sleep(1.0)
58 elif used_time > 0.1:
59 # Few GUI events coming in, so we can sleep longer
60 # print 'Sleep for 0.05 s' # dbg
61 time.sleep(0.05)
62 else:
63 # Many GUI events coming in, so sleep only very little
64 time.sleep(0.001)
65 except KeyboardInterrupt:
66 pass

Callers

nothing calls this directly

Calls 2

clockFunction · 0.85
flipFunction · 0.70

Tested by

no test coverage detected