您不能共享即使在python关闭后,内存中的任何python对象。但是,您可以使用pickle
保存对象的状态(如果库确实支持它,如果不支持,则此解决方案不起作用)。 pickle
是一个附带python的库,可以将大多数对象的状态保存到文件中。
下面是泡菜的例子:
要保存状态:
import pickle
obj = [1, 2, 3, 4]
f = open("/path/to/the/file/where/the/data/should/be/stored.pickle", 'wb') # you do not have to use the suffix '.pickle' however.
pickle.dump(obj, f)
f.close()
找回它:
import pickle
f = open("/path/to/the/file/where/the/data/should/be/stored.pickle", 'rb')
obj = pickle.load(f)
f.close()
或者为你的榜样,一旦运行这个事情:
from tzwhere import tzwhere
import pickle
f = open("/path/to/the/file", 'wb')
pickle.dump(tzwhere.tzwhere(shapely=True))
f.close()
并使用硫s到检索:
import pickle
f = open("/path/to/the/file", 'rb')
tz = pickle.load(f)
f.close()
或者作为一个内胆,所以它不会占用太大的空间:
import pickle;f=open("/path/to/the/file",'rb');tz=pickle.load(f);f.close()
我希望帮助,
CodenameLambda
PS:如果你想知道咸菜是如何工作的,可以看看the documentation。
@mevius先生,非常感谢你编辑这篇文章。我是一个新手,我不知道如何专业地写... –
你可以编写一个加载tz结构的服务器程序,监听请求,处理它们并发送回复。脚本将是该服务器的客户端。然而这并不重要。 – VPfB
@PrabhatNagpal对不起,但你必须等那么久。腌制对象没有意义,存储在'tzwhere'对象中的数据是巨大的。这就是为什么它需要很长时间才能加载。我会研究@VPFB的答案。 – CodenameLambda