背景信息:开始HERE!在
我有一个非常大的模块,可以从互联网上获取东西,其他内置脚本等等。根据网络速度、内存以及编译列表等因素,导入时间可以在25秒到90秒之间变化。我使用以下代码跟踪模块导入所需的时间:
def importTime():
import time
startTime = time.time()
import tms # This is the name of my module
print("Time taken {}".format(time.time() - startTime)))
当我运行这个:
^{pr2}$这就是我想要的:
>>> import tms
Loading Module. This may take up to 60 seconds.
Time taken: 31.49 seconds
这是我的问题。这是一个在导入模块之前必须定义的函数。我需要做的是让我的模块能够在启动时做到这一点。我已经看了一下这个question,但它是相同的概念。有人有什么想法吗?在
您可以重载导入模块时调用的^{} 函数:
在模块导入上做大量的工作通常是不可取的——这会严重破坏文档扫描仪、IDE、单元测试框架等。理想情况下,
tms
应该重写以在函数中执行其工作。但要解决您的问题,只需编写一个导入模块的简短模块。您甚至可以将其命名为tms
,并重命名原始导入程序,以便其他导入程序获得相同的功能(如果需要的话)。在在tmsx.py公司在
现在只需导入tmsx
^{pr2}$相关问题 更多 >
编程相关推荐