Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-3606

Document best practice for closing MongoClients and cursors

    • Type: Icon: Task Task
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • 4.11
    • Affects Version/s: None
    • Component/s: Docs
    • None
    • Needed
    • Hide

      Related to this post: https://www.mongodb.com/community/forums/t/i-am-using-pymongo-do-i-have-to-close-a-mongoclient-after-use/213511

      We should document that the best practice is to call close() when the client is no longer needed or use the client in a with-statement:

      with MongoClient(url) as client:

      1. use client here
        Also document that the best practice is to call close() on the cursor too, the best way would be to use a with-statement like this:

      with collection.find() as cursor:
      for s in cursor:
      print(s)

      All of our code examples that use a client or cursor should explicitly close it or use it as a context manager, unless the example is an open-ended snippet.

      There should be a short blurb similar to the following on the common issues troubleshooting page, the FAQ, and a note on the introduction page when MongoClient is first used in an example.

      When PyMongo's MongoClient and cursor objects are garbage collected without being closed pymongo will clean up application-side resources automatically (connections, threads, etc...) however some server-side resources could be left open (cursors, sessions). Calling close() explicitly (or using a with-statement) will clean up these server-side resources that would otherwise be left around until they timeout.

      Show
      Related to this post: https://www.mongodb.com/community/forums/t/i-am-using-pymongo-do-i-have-to-close-a-mongoclient-after-use/213511 We should document that the best practice is to call close() when the client is no longer needed or use the client in a with-statement: with MongoClient(url) as client: use client here Also document that the best practice is to call close() on the cursor too, the best way would be to use a with-statement like this: with collection.find() as cursor: for s in cursor: print(s) All of our code examples that use a client or cursor should explicitly close it or use it as a context manager, unless the example is an open-ended snippet. There should be a short blurb similar to the following on the common issues troubleshooting page, the FAQ, and a note on the introduction page when MongoClient is first used in an example. When PyMongo's MongoClient and cursor objects are garbage collected without being closed pymongo will clean up application-side resources automatically (connections, threads, etc...) however some server-side resources could be left open (cursors, sessions). Calling close() explicitly (or using a with-statement) will clean up these server-side resources that would otherwise be left around until they timeout.

      Related to this post: https://www.mongodb.com/community/forums/t/i-am-using-pymongo-do-i-have-to-close-a-mongoclient-after-use/213511

      We should document that the best practice is to call close() when the client is no longer needed or use the client in a with-statement:

      with MongoClient(url) as client:
          # use client here
      

      Also document that the best practice is to call close() on the cursor too, the best way would be to use a with-statement like this:

      with collection.find() as cursor:
          for s in cursor:
              print(s)
      

      When PyMongo's MongoClient and cursor objects are garbage collected without being closed pymongo will clean up application-side resources automatically (connections, threads, etc...) however some server-side resources could be left open (cursors, sessions). Calling close() explicitly (or using a with-statement) will clean up these server-side resources that would otherwise be left around until they timeout.

            Assignee:
            Unassigned Unassigned
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: