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