且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

每次重新加载python模块时如何避免计算

更新时间:2023-12-04 19:17:16

请澄清一下:每次导入模块时,模块主体中的代码不执行-仅运行一次,之后,将来的导入将找到已创建的模块,而不是重新创建它.查看sys.modules,以查看缓存的模块列表.

Just to clarify: the code in the body of a module is not executed every time the module is imported - it is run only once, after which future imports find the already created module, rather than recreating it. Take a look at sys.modules to see the list of cached modules.

但是,如果您的问题是程序运行后第一次导入所花费的时间,则可能需要使用除python dict以外的其他方法.***是使用磁盘上的表单,例如sqlite数据库,这是dbm模块之一.

However, if your problem is the time it takes for the first import after the program is run, you'll probably need to use some other method than a python dict. Probably best would be to use an on-disk form, for instance a sqlite database, one of the dbm modules.

要在界面上进行最小的更改,***使用shelve模块-在dbm模块之间放置一个非常透明的接口,使它们像任意python dict一样工作,允许存储任何可拾取的值.这是一个示例:

For a minimal change in your interface, the shelve module may be your best option - this puts a pretty transparent interface between the dbm modules that makes them act like an arbitrary python dict, allowing any picklable value to be stored. Here's an example:

# Create dict with a million items:
import shelve
d = shelve.open('path/to/my_persistant_dict')
d.update(('key%d' % x, x) for x in xrange(1000000))
d.close()

然后在下一步中使用它.应该没有大的延迟,因为只对磁盘上的表单上请求的键执行查找,因此不必将所有内容都加载到内存中.

Then in the next process, use it. There should be no large delay, as lookups are only performed for the key requested on the on-disk form, so everything doesn't have to get loaded into memory:

>>> d = shelve.open('path/to/my_persistant_dict')
>>> print d['key99999']
99999

比实际命令要慢一些,如果您执行需要所有键的操作(例如尝试打印它),它仍然需要很长的时间才能加载,但可以解决你的问题.

It's a bit slower than a real dict, and it will still take a long time to load if you do something that requires all the keys (eg. try to print it), but may solve your problem.