Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: significantly speed up the server launching time by async preloading tool providers #9146

Merged
merged 6 commits into from
Oct 30, 2024

Conversation

bowenliang123
Copy link
Contributor

@bowenliang123 bowenliang123 commented Oct 10, 2024

Checklist:

Important

Please review the checklist below before submitting your pull request.

  • Please open an issue before creating a PR or link to an existing issue
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Description

  • aync preloading the tool providers, which is thread-safe as it does check the status and locking
    (Tested on Apple MBP with M1pro chip)

  • Before:

image
  • after:
image

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update, included: Dify Document
  • Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement
  • Dependency upgrade

Testing Instructions

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Test A
  • Test B

@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. 💪 enhancement New feature or request size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Oct 10, 2024
@bowenliang123
Copy link
Contributor Author

bowenliang123 commented Oct 10, 2024

It has been tested on the local machine. Greatly helpful for development use and shrinking the restart time for production re-deployment.
cc @crazywoola @laipz8200

@laipz8200
Copy link
Member

We plan to address this issue more thoroughly, so this PR will not be merged.

@bowenliang123
Copy link
Contributor Author

It's still worth considering as a quick fix with no side effects before a thorough refactoring.

@laipz8200
Copy link
Member

Have you tried using separate threads to accomplish this function? In our environment, threads are managed by gevent, which helps maintain a consistent coding style.

@bowenliang123
Copy link
Contributor Author

In current implementation, asyncio's implicit event loop is used. It is dispatching an async task rather than holding a new dedicated thread to prevent over-design left unused for the rest time. As for the gevent , it would be considered if we do want a separate thread here, and also gevent's patching adapts the usages on standard library cooperative by default .

@laipz8200
Copy link
Member

In current implementation, asyncio's implicit event loop is used. It is dispatching an async task rather than holding a new dedicated thread to prevent over-design left unused for the rest time. As for the gevent , it would be considered if we do want a separate thread here, and also gevent's patching adapts the usages on standard library cooperative by default .

Sorry for the misunderstanding. What I meant is that we can use the standard library’s threading here, and gevent will handle converting it to asynchronous behavior. I would prefer not to introduce the asyncio module at this point.

@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Oct 30, 2024
@dosubot dosubot bot added size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Oct 30, 2024
@yaoice
Copy link
Contributor

yaoice commented Oct 30, 2024

Well done! This startup acceleration is awesome.

@laipz8200 laipz8200 self-requested a review October 30, 2024 15:12
@laipz8200 laipz8200 self-assigned this Oct 30, 2024
@bowenliang123
Copy link
Contributor Author

Have changed to use simple threading to execute the async task. Please have a review on it.

Copy link
Member

@laipz8200 laipz8200 left a comment

Choose a reason for hiding this comment

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

LGTM

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Oct 30, 2024
@laipz8200 laipz8200 merged commit 0bdae34 into langgenius:main Oct 30, 2024
6 checks passed
@bowenliang123 bowenliang123 deleted the async-preload-tools branch October 30, 2024 17:05
@bowenliang123
Copy link
Contributor Author

Thanks for the reconsideration and merging.

Nov1c444 pushed a commit that referenced this pull request Nov 5, 2024
iamjoel pushed a commit that referenced this pull request Nov 7, 2024
idonotknow pushed a commit to AceDataCloud/Dify that referenced this pull request Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 enhancement New feature or request lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants