导入pyglet.resource在大型目录中占用过多的时间

时间:2014-06-10 03:18:29

标签: python python-2.7 pyglet

从具有大型目录树结构的目录运行时,导入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

1 个答案:

答案 0 :(得分:0)

似乎在Pyglet中将此问题报告为issue #367;它已在revision 9664bc47446c中修复(在1.1.4版之后)。