Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
1f58f15b
Unverified
Commit
1f58f15b
authored
Nov 22, 2023
by
takatost
Committed by
GitHub
Nov 22, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: optimize db connections in thread (#1601)
parent
b9307167
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
19 deletions
+22
-19
completion_service.py
api/services/completion_service.py
+22
-19
No files found.
api/services/completion_service.py
View file @
1f58f15b
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment