Skip to content
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

SQLite objects created in a thread can only be used in that same thread #180

Closed
Vosloo opened this issue Dec 1, 2022 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@Vosloo
Copy link

Vosloo commented Dec 1, 2022

Problem description

Hello, I am trying to create a micro-service (with Flask) that use the library for getting synsets of a given word. I've noticed some unexpected behaviour when creating quering for synsets and Flask. For the attached code below I am getting an error:

ProgrammingError('SQLite objects created in a thread can only be used in that same thread.
The object was created in thread id 139815518537536 and this is thread id 139811777947392.

Code to reproduce

# foo.py
import wn
from wn.morphy import Morphy

class Foo:
    def __init__(self):
        self.wnde, self.morphy = self._initialize_wn()

    def _initialize_wn(self) -> tuple[wn.Wordnet, Morphy]:
        print("Initializing Wordnet...")
        wn.download("odenet")

        wnde = wn.Wordnet(lang="de")
        morphy = Morphy(wnde)
        wnde.lemmatizer = morphy

        return wnde, morphy

    def get_synsets(self, word: str) -> list:
        return self.wnde.synsets(word)
from flask import Flask
from foo import Foo

app = Flask(__name__)
foo = Foo()

@app.route("/", methods=["POST])
def do_stuff():
    word = request.form["data"]
    return {"data": foo.generate(word)}, 200

if __name__ == "__main__":
    app.run(
        threaded=True,  # Doesn't matter here if True or False, error still present
        host="0.0.0.0",
        debug=True,
    )

Expected behavior

We should get a list of synsets.

Environment

  • Python 3.10.5
  • Wn 0.9.3
  • Lexicons:
    -- odenet 1.4 [de] Offenes Deutsches WordNet (only using this one)
    -- oewn 2021 [en] Open English WordNet
@Vosloo Vosloo added the bug Something isn't working label Dec 1, 2022
@goodmami
Copy link
Owner

goodmami commented Dec 3, 2022

@Vosloo thanks for the detailed report. SQLite doesn't allow multithreading by default. Have you tried using the wn.config.allow_multithreading option?

import wn
wn.config.allow_multithreading = True

Also see #86.

@goodmami
Copy link
Owner

goodmami commented Dec 4, 2022

I'm going to close this, but feel free to reopen if my suggestion above doesn't work. I tried it using your code to reproduce the issue (which BTW wasn't usable without a few fixes) and I got around the ProgrammingError, so I'll assume that this fixes the problem.

@goodmami goodmami closed this as completed Dec 4, 2022
@Vosloo
Copy link
Author

Vosloo commented Dec 6, 2022

I wasn't aware of such an option and it does indeed work, thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants