你可以使用任何pickle模块来pickle方法python方法对象吗?
我有一个长时间运行的脚本,它执行的操作可以分解为独立但顺序敏感的函数调用。此外,这里的模式是面包优先遍历图表,并且在崩溃后重新启动深度并不是一个好的选择。
那么,我的想法如下:当脚本运行时,维护一个函数参数对列表。该列表对应于图的深度优先搜索的打开列表。数据很容易访问,所以我不需要担心丢失它。
如果由于网络条件或错误而发生崩溃(可能不可避免),函数参数对列表足以恢复操作。
那么,我可以存储这些功能吗?我的直觉告诉我不会,但在我做出任何判断之前我需要一些建议。
I've got a long-running script, and the operations that it performs can be broken down into independent, but order-sensitive function calls. In addition, the pattern here is a bread-first walk over a graph, and restarting a depth after a crash is not a happy option.
So then, my idea is as follows: as the script runs, maintain a list of function-argument pairs. This list corresponds to the open list of a depth-first search of the graph. The data is readily accessible, so I don't need to be concerned about losing it.
In the (probably unavoidable) event of a crash due to network conditions or an error, the list of function argument pairs is enough to resume the operation.
So then, can I store the functions? My intuition says no, but I'd like some advice before I judge either way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以腌制普通函数。 Python 存储名称和模块并使用它来重新加载它。如果你想对方法等做同样的事情,那就有点棘手了。这可以通过将这些物体分解成零部件并对其进行酸洗来完成。
You can pickle plain functions. Python stores the name and module and uses that to reload it. If you want to do the same with methods and such that's a little bit trickier. It can be done by breaking such objects down into component parts and pickling those.
如果参数才是最重要的,为什么不存储参数呢?只需将其存储为直元组即可。
If the arguments are all that matters, why not store the arguments? Just store it as a straight tuple.