从具有大型目录树结构的目录运行时,导入pyglet.resource
需要很长时间。它发生在简单的单行代码:import pyglet.resource
,它在一个足够大的目录(比如我的主目录)中只会导致解释器挂起。
如果导入被 C-c 中断,则会打印以下回溯,表明正在遍历整个目录树:
>>> import pyglet.resource
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/pyglet/__init__.py", line 306, in __getattr__
__import__(import_name)
File "/usr/lib/pymodules/python2.7/pyglet/resource.py", line 680, in <module>
_default_loader = _DefaultLoader()
File "/usr/lib/pymodules/python2.7/pyglet/resource.py", line 291, in __init__
self.reindex()
File "/usr/lib/pymodules/python2.7/pyglet/resource.py", line 335, in reindex
for dirpath, dirnames, filenames in os.walk(path):
File "/usr/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/usr/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/usr/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/usr/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/usr/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/usr/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/usr/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/usr/lib/python2.7/os.py", line 294, in walk
for x in walk(new_path, topdown, onerror, followlinks):
File "/usr/lib/python2.7/os.py", line 284, in walk
if isdir(join(top, name)):
File "/usr/lib/python2.7/genericpath.py", line 41, in isdir
st = os.stat(s)
KeyboardInterrupt
(可调数量walk
次调用,具体取决于脚本运行的时间长度)
这很令人惊讶,因为导入pyglet.resource实际上不需要遍历脚本的目录。造成这种情况的原因是什么,可以修复还是解决?
版本信息:Python 2.7,Pyglet 1.1.4,Ubuntu 12.04
此问题似乎已在this pyglet-users讨论中进行了描述,其中错误地将其归因于构造pyglet.resource.FileLocation
对象。但是,问题实际上发生在像
import pyglet
...
pyglet.resource.FileLocation(...) # pyglet.resource is imported here;
# FileLocation is not the problem