Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/application/flow/workflow_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from functools import reduce
from typing import List, Dict

from django.db import close_old_connections
from django.db import close_old_connections, connection
from django.db.models import QuerySet
from django.utils import translation
from django.utils.translation import get_language
Expand Down Expand Up @@ -569,6 +569,8 @@ def hand_event_node_result(self, current_node, node_result_future):
return None
finally:
current_node.node_chunk.end()
# 归还链接
connection.close()

def run_node_async(self, node):
future = executor.submit(self.run_node, node)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no irregularities in the given code snippet. Here are some minor recommendations for improvement:

# Import necessary modules at the beginning to avoid circular imports or potential issues
from functools import reduce
from typing import List, Dict

import django
django.setup()  # Optional setup if not done elsewhere

from django.db import close_old_connections
from django.db.models import QuerySet
from django.utils import translation

The changes ensure that Django is properly initialized before being used, which might be important for certain operations depending on your use case.

Additionally, it's generally a good practice to check if connection has been defined before calling its methods. However, since you're closing it immediately after, this check won't apply but could serve as a reminder:

try:
    result = self.hand_event_node_result(
        current_node,
        node_result_future)
except Exception as e:
    print(f"An error occurred while processing {current_node}: {e}")
finally:
    try:
        close_old_connections()
        # If using async tasks, make sure to wait for all futures to complete
        node_result_future.result()
    except Exception as e:
        print("Exception during cleanup")
    finally:
        current_node.node_chunk.end()
        # Optionally, check if 'connection' exists before closing in production
        if hasattr(connection, 'close'):
            connection.close()

Make sure that when running these tasks asynchronously, you handle errors appropriately and possibly call .result() on the future to ensure all operations have completed before cleaning up resources.

Expand Down
Loading