rethinkdb / rethinkdb-python

Python driver for RethinkDB
https://rethinkdb.com/api/python/
Apache License 2.0
64 stars 34 forks source link

Proposal: Disconnect connection when garbage collected #268

Open TheTechRobo opened 2 years ago

TheTechRobo commented 2 years ago

Is your feature request related to a problem? Please describe. In my program, I didn't know that you need to call conn.disconnect() when you are finished with a connection, causing me to frequently hit the "Too many open files" error (I opened too many connections to the RethinkDB server).

Describe the solution you'd like I believe that in classes there is the __del__ function, called when the object is freed. Alternatively, there's also the weakref.finalize function (example). Using this, we could disconnect when the connection is garbage collected. Finally, r.connect could be turned into a context manager, but that is a breaking change.

Note that I'm not familiar with any of RethinkDB's code, so please tell me if this would have unintended side effects or backwards incompatibility. I'm also not fully sure if this solution would work, so correct me if I'm wrong.

Describe alternatives you've considered We could add something like "Make sure to disconnect when you're done." to the ten-minute guide. Right now it does not mention disconnecting at all.

TheTechRobo commented 2 years ago

Sorry, I didn't see the "no feature requests" in the README.