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

Function inputhook_pyglet

IPython/lib/inputhookpyglet.py:68–111  ·  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

()

Source from the content-addressed store, hash-verified

66#-----------------------------------------------------------------------------
67
68def inputhook_pyglet():
69 """Run the pyglet event loop by processing pending events only.
70
71 This keeps processing pending events until stdin is ready. After
72 processing all pending events, a call to time.sleep is inserted. This is
73 needed, otherwise, CPU usage is at 100%. This sleep time should be tuned
74 though for best performance.
75 """
76 # We need to protect against a user pressing Control-C when IPython is
77 # idle and this is running. We trap KeyboardInterrupt and pass.
78 try:
79 t = clock()
80 while not stdin_ready():
81 pyglet.clock.tick()
82 for window in pyglet.app.windows:
83 window.switch_to()
84 window.dispatch_events()
85 window.dispatch_event('on_draw')
86 flip(window)
87
88 # We need to sleep at this point to keep the idle CPU load
89 # low. However, if sleep to long, GUI response is poor. As
90 # a compromise, we watch how often GUI events are being processed
91 # and switch between a short and long sleep time. Here are some
92 # stats useful in helping to tune this.
93 # time CPU load
94 # 0.001 13%
95 # 0.005 3%
96 # 0.01 1.5%
97 # 0.05 0.5%
98 used_time = clock() - t
99 if used_time > 10.0:
100 # print 'Sleep for 1 s' # dbg
101 time.sleep(1.0)
102 elif used_time > 0.1:
103 # Few GUI events coming in, so we can sleep longer
104 # print 'Sleep for 0.05 s' # dbg
105 time.sleep(0.05)
106 else:
107 # Many GUI events coming in, so sleep only very little
108 time.sleep(0.001)
109 except KeyboardInterrupt:
110 pass
111 return 0

Callers

nothing calls this directly

Calls 3

clockFunction · 0.85
stdin_readyFunction · 0.70
flipFunction · 0.70

Tested by

no test coverage detected