我正在编写一个科学计算领域的Python应用程序。目前,当用户使用图形用户界面并启动新的物理模拟时,解释器会立即导入此模拟所需的几个模块,如Traits和Mayavi。这些模块很重,而且导入时间太长,用户必须等待大约10秒才能继续,这是很糟糕的。
我想到了一些可以弥补这个问题的方法。我将描述它,也许其他人已经实现了它,如果是这样,请给我一个链接。如果不是,我可能会自己去做。
我想要的是一个单独的线程,它将异步导入模块。它可能是threading.Thread的一个子类。
下面是一个使用示例:
importer_thread = ImporterThread()
importer_thread.start()
# ...
importer_thread.import('Mayavi')
importer_thread.import('Traits')
# A thread-safe method that will put the module name
# into a queue which the thread in an inifine loop
# ...
# When the user actually needs the modules:
import Mayavi, Traits
# If they were already loaded by importer_thread, we're good.
# If not, we'll just have to wait as usual.你知道有这样的事情吗?如果没有,你对设计有什么建议吗?
发布于 2010-01-15 21:31:46
这样做的问题是,导入仍然必须完成,然后才能使用。根据第一次使用它们的时间,应用程序可能仍然必须阻塞10秒,然后才能启动。更有效的方法是对模块进行概要分析,并找出它们为什么需要这么长时间才能导入。
https://stackoverflow.com/questions/2071786
复制相似问题