-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create one database connection per thread #720
Create one database connection per thread #720
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @tkrabel, thanks for making this PR, there's one issue with the code currently as they are. Once they are fixed, I think it should be ready to go.
rope/contrib/autoimport/sqlite.py
Outdated
@@ -67,6 +68,7 @@ def filter_package(package: Package) -> bool: | |||
|
|||
|
|||
_deprecated_default: bool = object() # type: ignore | |||
thread_local = local() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, creating the thread_local at the module-level like this would mean that all instances of autoimports would share the same database and database connection, even though they belong to different instances of AutoImport and more importantly that they belong to different Projects:
project_a = Project("/path/to/project/a")
project_b = Project("/path/to/project/b")
ai_a = AutoImport(project_a, ...)
ai_b = AutoImport(project_b, ...)
assert ai_a.connection is not ai_b.connection
That does not look correct to me. I think this can be avoided by moving thread_local
to be an instance variable initialized in AutoImport.__init__()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing that our! You're right. I moved the thread local to the __init__
and wrote a test that verifies the following passes:
# Test connections
from rope.base.project import Project
from rope.contrib.autoimport.sqlite import AutoImport
ai1 = AutoImport(Project(".."))
ai2 = AutoImport(Project("."))
ai3 = AutoImport(Project("."))
assert ai1.connection is not ai2.connection
assert ai1.connection is not ai3.connection
Looks good to me, thanks for the contribution @tkrabel. @all-contributors add @tkrabel for code contribution |
I couldn't determine any contributions to add, did you specify any contributions? I've put up a pull request to add @tkrabel! 🎉 |
Description
Addresses #714 (comment), using a threadlocal to create data base connections.
Fixes #713
Checklist (delete if not relevant):