如果神圣无法连接到MongoDB,如何导入pickle文件

时间:2017-07-02 08:39:22

标签: python mongodb pymongo pickle python-sacred

实验软件sacred在后台运行时没有配置mongo-observer的MongoDB。当它尝试将设置写入MongoDB时,此操作失败,创建文件/tmp/sacred_mongo_fail__eErwU.pickle,并显示消息

Warning: saving to MongoDB failed! Stored experiment entry in /tmp/sacred_mongo_fail__eErwU.pickle
Traceback (most recent calls WITHOUT Sacred internals):
  File "/usr/local/lib/python2.7/dist-packages/sacred/observers/mongo.py", line 127, in started_event
    self.run_entry[experiment][sources] = self.save_sources(ex_info)
  File "/usr/local/lib/python2.7/dist-packages/sacred/observers/mongo.py", line 239, in save_sources
    file = self.fs.find_one({filename: abs_path, md5: md5})
  File "/usr/local/lib/python2.7/dist-packages/gridfs/__init__.py", line 261, in find_one
    for f in self.find(filter, *args, **kwargs):
  File "/usr/local/lib/python2.7/dist-packages/gridfs/grid_file.py", line 658, in next
    next_file = super(GridOutCursor, self).next()
  File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 1114, in next
    if len(self.__data) or self._refresh():
  File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 1036, in _refresh
    self.__collation))
  File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 873, in __send_message
    **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/mongo_client.py", line 888, in _send_message_with_response
    server = topology.select_server(selector)
  File "/usr/local/lib/python2.7/dist-packages/pymongo/topology.py", line 214, in select_server
    address))
  File "/usr/local/lib/python2.7/dist-packages/pymongo/topology.py", line 189, in select_servers
    self._error_message(selector))
ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

如何将这个pickle文件手动导入MongoDB?

1 个答案:

答案 0 :(得分:0)

  1. 加载pickle文件,
  2. 设置_id
  3. 插入
  4. db = pymongo.MongoClient().sacred
    entry = pickle.load(open('/tmp/sacred_mongo_fail__eErwU.pickle'))
    entry['_id'] = list(db.runs.find({}, {"_id": 1}))[-1]['_id']
    db.runs.insert_one(entry)
    

    这很快且很脏,取决于find按顺序列出对象,并且可以使用Cleanest way to get last item from Python iterator代替list(...)[-1],但它应该有用。