Unverified Commit 1f58f15b authored by takatost's avatar takatost Committed by GitHub

feat: optimize db connections in thread (#1601)

parent b9307167
...@@ -232,7 +232,7 @@ class CompletionService: ...@@ -232,7 +232,7 @@ class CompletionService:
logging.exception("Unknown Error in completion") logging.exception("Unknown Error in completion")
PubHandler.pub_error(user, generate_task_id, e) PubHandler.pub_error(user, generate_task_id, e)
finally: finally:
db.session.commit() db.session.remove()
@classmethod @classmethod
def countdown_and_close(cls, flask_app: Flask, worker_thread, pubsub, detached_user, def countdown_and_close(cls, flask_app: Flask, worker_thread, pubsub, detached_user,
...@@ -242,22 +242,25 @@ class CompletionService: ...@@ -242,22 +242,25 @@ class CompletionService:
def close_pubsub(): def close_pubsub():
with flask_app.app_context(): with flask_app.app_context():
user = db.session.merge(detached_user) try:
user = db.session.merge(detached_user)
sleep_iterations = 0
while sleep_iterations < timeout and worker_thread.is_alive(): sleep_iterations = 0
if sleep_iterations > 0 and sleep_iterations % 10 == 0: while sleep_iterations < timeout and worker_thread.is_alive():
PubHandler.ping(user, generate_task_id) if sleep_iterations > 0 and sleep_iterations % 10 == 0:
PubHandler.ping(user, generate_task_id)
time.sleep(1)
sleep_iterations += 1 time.sleep(1)
sleep_iterations += 1
if worker_thread.is_alive():
PubHandler.stop(user, generate_task_id) if worker_thread.is_alive():
try: PubHandler.stop(user, generate_task_id)
pubsub.close() try:
except Exception: pubsub.close()
pass except Exception:
pass
finally:
db.session.remove()
countdown_thread = threading.Thread(target=close_pubsub) countdown_thread = threading.Thread(target=close_pubsub)
countdown_thread.start() countdown_thread.start()
...@@ -394,7 +397,7 @@ class CompletionService: ...@@ -394,7 +397,7 @@ class CompletionService:
logging.exception(e) logging.exception(e)
raise raise
finally: finally:
db.session.commit() db.session.remove()
try: try:
pubsub.unsubscribe(generate_channel) pubsub.unsubscribe(generate_channel)
...@@ -436,7 +439,7 @@ class CompletionService: ...@@ -436,7 +439,7 @@ class CompletionService:
logging.exception(e) logging.exception(e)
raise raise
finally: finally:
db.session.commit() db.session.remove()
try: try:
pubsub.unsubscribe(generate_channel) pubsub.unsubscribe(generate_channel)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment