##// END OF EJS Templates
Trap AttributeError in window.flip() under linux - pyglet bug.
Fernando Perez -
Show More
@@ -1,93 +1,115 b''
1 # encoding: utf-8
1 # encoding: utf-8
2
3 """
2 """
4 Enable pyglet to be used interacive by setting PyOS_InputHook.
3 Enable pyglet to be used interacive by setting PyOS_InputHook.
5
4
6 Authors: Nicolas P. Rougier
5 Authors
6 -------
7
8 * Nicolas P. Rougier
9 * Fernando Perez
7 """
10 """
8
11
9 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
10 # Copyright (C) 2008-2009 The IPython Development Team
13 # Copyright (C) 2008-2009 The IPython Development Team
11 #
14 #
12 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
15
18
16 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
17 # Imports
20 # Imports
18 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
19
22
20 import os
23 import os
21 import signal
24 import signal
22 import sys
25 import sys
23 import time
26 import time
24 from timeit import default_timer as clock
27 from timeit import default_timer as clock
25 import pyglet
28 import pyglet
26
29
27 if os.name == 'posix':
28 import select
29 elif sys.platform == 'win32':
30 import msvcrt
31
32 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
33 # Code
31 # Platform-dependent imports and functions
34 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
35
33
36 def stdin_ready():
34 if os.name == 'posix':
37 if os.name == 'posix':
35 import select
36
37 def stdin_ready():
38 infds, outfds, erfds = select.select([sys.stdin],[],[],0)
38 infds, outfds, erfds = select.select([sys.stdin],[],[],0)
39 if infds:
39 if infds:
40 return True
40 return True
41 else:
41 else:
42 return False
42 return False
43 elif sys.platform == 'win32':
43
44 elif sys.platform == 'win32':
45 import msvcrt
46
47 def stdin_ready():
44 return msvcrt.kbhit()
48 return msvcrt.kbhit()
45
49
46
50
51 # On linux only, window.flip() has a bug that causes an AttributeError on
52 # window close. For details, see:
53 # http://groups.google.com/group/pyglet-users/browse_thread/thread/47c1aab9aa4a3d23/c22f9e819826799e?#c22f9e819826799e
54
55 if sys.platform.startswith('linux'):
56 def flip(window):
57 try:
58 window.flip()
59 except AttributeError:
60 pass
61 else:
62 def flip(window):
63 window.flip()
64
65 #-----------------------------------------------------------------------------
66 # Code
67 #-----------------------------------------------------------------------------
68
47 def inputhook_pyglet():
69 def inputhook_pyglet():
48 """Run the pyglet event loop by processing pending events only.
70 """Run the pyglet event loop by processing pending events only.
49
71
50 This keeps processing pending events until stdin is ready. After
72 This keeps processing pending events until stdin is ready. After
51 processing all pending events, a call to time.sleep is inserted. This is
73 processing all pending events, a call to time.sleep is inserted. This is
52 needed, otherwise, CPU usage is at 100%. This sleep time should be tuned
74 needed, otherwise, CPU usage is at 100%. This sleep time should be tuned
53 though for best performance.
75 though for best performance.
54 """
76 """
55 # We need to protect against a user pressing Control-C when IPython is
77 # We need to protect against a user pressing Control-C when IPython is
56 # idle and this is running. We trap KeyboardInterrupt and pass.
78 # idle and this is running. We trap KeyboardInterrupt and pass.
57 try:
79 try:
58 t = clock()
80 t = clock()
59 while not stdin_ready():
81 while not stdin_ready():
60 pyglet.clock.tick()
82 pyglet.clock.tick()
61 for window in pyglet.app.windows:
83 for window in pyglet.app.windows:
62 window.switch_to()
84 window.switch_to()
63 window.dispatch_events()
85 window.dispatch_events()
64 window.dispatch_event('on_draw')
86 window.dispatch_event('on_draw')
65 window.flip()
87 flip(window)
66
88
67 # We need to sleep at this point to keep the idle CPU load
89 # We need to sleep at this point to keep the idle CPU load
68 # low. However, if sleep to long, GUI response is poor. As
90 # low. However, if sleep to long, GUI response is poor. As
69 # a compromise, we watch how often GUI events are being processed
91 # a compromise, we watch how often GUI events are being processed
70 # and switch between a short and long sleep time. Here are some
92 # and switch between a short and long sleep time. Here are some
71 # stats useful in helping to tune this.
93 # stats useful in helping to tune this.
72 # time CPU load
94 # time CPU load
73 # 0.001 13%
95 # 0.001 13%
74 # 0.005 3%
96 # 0.005 3%
75 # 0.01 1.5%
97 # 0.01 1.5%
76 # 0.05 0.5%
98 # 0.05 0.5%
77 used_time = clock() - t
99 used_time = clock() - t
78 if used_time > 5*60.0:
100 if used_time > 5*60.0:
79 # print 'Sleep for 5 s' # dbg
101 # print 'Sleep for 5 s' # dbg
80 time.sleep(5.0)
102 time.sleep(5.0)
81 elif used_time > 10.0:
103 elif used_time > 10.0:
82 # print 'Sleep for 1 s' # dbg
104 # print 'Sleep for 1 s' # dbg
83 time.sleep(1.0)
105 time.sleep(1.0)
84 elif used_time > 0.1:
106 elif used_time > 0.1:
85 # Few GUI events coming in, so we can sleep longer
107 # Few GUI events coming in, so we can sleep longer
86 # print 'Sleep for 0.05 s' # dbg
108 # print 'Sleep for 0.05 s' # dbg
87 time.sleep(0.05)
109 time.sleep(0.05)
88 else:
110 else:
89 # Many GUI events coming in, so sleep only very little
111 # Many GUI events coming in, so sleep only very little
90 time.sleep(0.001)
112 time.sleep(0.001)
91 except KeyboardInterrupt:
113 except KeyboardInterrupt:
92 pass
114 pass
93 return 0
115 return 0
General Comments 0
You need to be logged in to leave comments. Login now