1

I have a flask rest API. there is a function which repeats every minute and updates the database(sqlite3). It works for a while but after some time API gets unresponsive and won't respond to requests. I think this function makes the API unresponsive. I can see the error mentioned in the question title. Am I doing anything wrong here? Any help is appreciated.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import threading
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
.
.
.
def UpdateStatus():
threading.Timer(60, UpdateStatus).start() 
messages = Message.query.filter((Message.status != 'Seen')).all()
for msg in messages:
    try:
        messageid= msg.messageid
        wamessageid= msg.wamessageid
        timestamp= datetime.now()
        result= driver.get_message_status(wamessageid)
        if result==1:
            status='Sent'
            api_response= 'OK: Message sent'
        elif result==2:
            status= 'Delivered'
            api_response= 'OK: Message Delivered'
        elif result==3:
            status= 'Seen'
            api_response= 'OK: Message Seen'
        message = Message.query.filter_by(messageid = messageid).first()
        if message.status!= status:
            message.status= status
            message.apiresponse= api_response
            if status== 'Delivered':
                message.timestamp_delivered= timestamp
            if status== 'Seen':
                if message.timestamp_delivered is None:
                    message.timestamp_delivered= timestamp
                message.timestamp_seen= timestamp
            db.session.commit()
    except:
        traceback.print_exc()
        continue
majidse
  • 19
  • 4
  • what's the path of the db? – Joost Apr 27 '18 at 18:53
  • there is no problem with the path. I'm sure about that. because this function works for a while. but after some time I get this error. Anyway updated the code. – majidse Apr 27 '18 at 18:55
  • when this error first occurs, will some requests still get through? Or you get this error only once and afterwards it's completely stuck? Can you post the entire error message? – Joost Apr 27 '18 at 19:02
  • After I see this error no request goes through and it completely gets stuck. – majidse Apr 27 '18 at 19:04
  • Are you sure the db is still readable and writeable after it gets stuck? Because it sounds like the database file is dissapearing – Joost Apr 27 '18 at 19:05
  • Almost sure about that. Db file is there and I have no other function to interfere with the database. – majidse Apr 27 '18 at 19:08
  • Are you using threading in your app? – Fine Apr 28 '18 at 10:03
  • @Fian yes. I do – majidse Apr 28 '18 at 12:41
  • Then error may be caused by the db-file locking by one thread during writing (commiting) and simultaneous (concurrent) read/write attempt from the another thread. [See here for more.](https://stackoverflow.com/questions/10325683/can-i-read-and-write-to-a-sqlite-database-concurrently-from-multiple-connections) – Fine Apr 28 '18 at 12:49
  • may you please guide me how to enable WAL mode in sqlalchemy? – majidse Apr 28 '18 at 13:16
  • I've never used it. – Fine Apr 28 '18 at 13:55

0 Answers0