3

I saw a few other posts about this and understand what the problem is. However I'm struggling to write the code so it avoids the error:

from database import db_session
from database import init_db

database.py

engine = create_engine('sqlite:///'+full_path, convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()

def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    import models
    Base.metadata.create_all(bind=engine)

flaskserver.py

@app.route('/process_email')
def process_email():
    init_db()
    email_address = request.args.get('email_address')
    print(email_address)
    email_date =  datetime.now().strftime('%Y-%m-%d')
    rec = db_session.query(EmailCampaign).filter(EmailCampaign.email == email_address).first()
    if rec.email_4 is not None:
        rec.email_4_respond = email_date
    elif rec.email_3 is not None:
        rec.email_3_respond = email_date
    elif rec.email_2 is not None:
        rec.email_2_respond = email_date
    elif rec.email_1 is not None:
        rec.email_1_resonded = email_date

    db_session.commit()
    return('done')

I randomly get

sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 9396 and this is thread id 13360.

as an erorr. I think I have to initialize the connection inside the process_email() function right? I've tried it outside the function but also get the same error.

Thanks.

Morgan Allen
  • 3,291
  • 8
  • 62
  • 86
  • What command are you using to launch the flask app? – James Sep 09 '19 at 12:05
  • python flaskserver.py – Morgan Allen Sep 09 '19 at 12:30
  • 1
    Possible duplicate of [ProgrammingError: SQLite objects created in a thread can only be used in that same thread](https://stackoverflow.com/questions/48218065/programmingerror-sqlite-objects-created-in-a-thread-can-only-be-used-in-that-sa) – Joe Sep 09 '19 at 14:09
  • https://stackoverflow.com/a/48234567/7919597 – Joe Sep 09 '19 at 14:10
  • https://www.reddit.com/r/learnpython/comments/5cwx34/flask_sqlite_error/?utm_source=share&utm_medium=web2x – Joe Sep 09 '19 at 14:12
  • https://docs.sqlalchemy.org/en/13/dialects/sqlite.html#threading-pooling-behavior – Joe Sep 09 '19 at 14:16
  • @Joe I'm initializing the db connection in another file. How do I import it properly so I avoid this error? – Morgan Allen Sep 09 '19 at 15:25
  • https://docs.sqlalchemy.org/en/13/orm/session_basics.html – Joe Sep 09 '19 at 15:31
  • e.g. like https://stackoverflow.com/questions/31681644/accessing-same-db-session-across-different-modules-in-sqlalchemys – Joe Sep 09 '19 at 15:32
  • This might solve it: https://stackoverflow.com/a/34010159/7919597 – Joe Sep 09 '19 at 18:23
  • Ok, I solved it. The correct way is to use the `scoped_session` as described in https://docs.sqlalchemy.org/en/13/orm/contextual.html Depending on how you are using the database, there might be a module to handle this in an of-the-shelf way, e.g. for Flask there is [Flask-SQLAlchemy](https://flask-sqlalchemy.palletsprojects.com/en/2.x/) – Joe Sep 09 '19 at 18:53

0 Answers0