mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-15 16:05:55 +08:00
feat: optimize db connections in thread (#1601)
This commit is contained in:
parent
b930716745
commit
1f58f15bff
@ -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,6 +242,7 @@ class CompletionService:
|
|||||||
|
|
||||||
def close_pubsub():
|
def close_pubsub():
|
||||||
with flask_app.app_context():
|
with flask_app.app_context():
|
||||||
|
try:
|
||||||
user = db.session.merge(detached_user)
|
user = db.session.merge(detached_user)
|
||||||
|
|
||||||
sleep_iterations = 0
|
sleep_iterations = 0
|
||||||
@ -258,6 +259,8 @@ class CompletionService:
|
|||||||
pubsub.close()
|
pubsub.close()
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
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:
|
|||||||
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:
|
|||||||
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user