代码之家  ›  专栏  ›  技术社区  ›  AndreiVataselu

PyMongo-本地主机上的服务器超时

  •  0
  • AndreiVataselu  · 技术社区  · 6 年前

    我正在尝试学习如何在Python中使用MongoDB,但是每次我运行代码都会花费很长时间,以至于我不得不终止它,并得到这些回溯。

            "/Volumes/ADATA HD720/flapPY/venv/bin/python" "/Volumes/ADATA HD720/flapPY/Database.py"
    Traceback (most recent call last):
      File "/Volumes/ADATA HD720/flapPY/Database.py", line 8, in <module>
        x = high_scores.insert_one(mydict)
      File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/collection.py", line 693, in insert_one
        session=session),
      File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/collection.py", line 607, in _insert
        bypass_doc_val, session)
      File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/collection.py", line 595, in _insert_one
        acknowledged, _insert_command, session)
      File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1247, in _retryable_write
        with self._tmp_session(session) as s:
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 112, in __enter__
        return next(self.gen)
      File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1576, in _tmp_session
        s = self._ensure_session(session)
      File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1563, in _ensure_session
        return self.__start_session(True, causal_consistency=False)
      File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1516, in __start_session
        server_session = self._get_server_session()
      File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/mongo_client.py", line 1549, in _get_server_session
        return self._topology.get_server_session()
      File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/topology.py", line 427, in get_server_session
        None)
      File "/Volumes/ADATA HD720/flapPY/venv/lib/python3.7/site-packages/pymongo/topology.py", line 208, in _select_servers_loop
        self._condition.wait(common.MIN_HEARTBEAT_INTERVAL)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 300, in wait
        gotit = waiter.acquire(True, timeout)
    KeyboardInterrupt
    
    Process finished with exit code 1
    

    这是我的密码:

    import pymongo
    
    client = pymongo.MongoClient("localhost", 27017)
    database = client["database"]
    high_scores = database["highscore"]
    
    mydict = {"highscore" : 5}
    x = high_scores.insert_one(mydict)
    print(x.inserted_id)
    

    我认为它无法连接到localhost

    1 回复  |  直到 6 年前
        1
  •  0
  •   AndreiVataselu    6 年前

    brew services start mongodb
    

    而且效果很好。