-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
[Bug]: Using flask restful server and got [Session and connector has to use same event loop] error #13426
Comments
Hey there, @mglslg! 😄 Long time, no bug, huh? It's always a pleasure to see your name pop up, even if it's accompanied by a tricky issue. I'm diving into this headfirst to untangle it for you, so hang tight! Will get back to you with a full answer shortly. |
My first suggestion is to rewrite your api endpoint using actual async, but I'm not sure how async in flask works (I typically use fastapi) Alternatively, seems like an internal issue in the elastic search client |
@logan-markewich Thank you for your help :) Yes, it is indeed an issue with Elasticsearch itself, but I also tried using a custom-created Elasticsearch synchronized client and it still doesn't work. Then I added nest_asyncio.apply() in the rest API code, but it still throw the same error. Currently, I am not very keen on switching the app to another async-supported framework. Is there any other way to solve this issue? |
To address the "RuntimeError: Session and connector has to use same event loop" error in your Flask application without switching to an async-supported framework, you'll need to ensure that your Elasticsearch queries are executed in a synchronous manner. This involves avoiding direct use of asynchronous methods within Flask route handlers, which are not compatible with Flask's synchronous nature. Here's a strategy to adapt your Elasticsearch operations to be synchronous within Flask:
By focusing on ensuring that all Elasticsearch queries and related operations are executed in a synchronous context within your Flask application, you can avoid the event loop error and ensure compatibility with Flask's execution model.
|
@dosubot Then how can I specify using a synchronous Elasticsearch client in LlamaIndex? |
To use a synchronous Elasticsearch client in LlamaIndex within a Flask application, you can adapt asynchronous operations to run synchronously. This involves wrapping asynchronous method calls with
By following these steps, you can maintain your Flask application's synchronous execution model while performing asynchronous Elasticsearch operations under the hood. This method leverages the power of asynchronous IO with Elasticsearch without needing to switch your entire application to an asynchronous framework.
|
@dosubot I tried to use synchronized client like this:
But got this exception:
So, is llamaindex not supporting synchronous ES client? |
It seems like the core of the issue you're facing with LlamaIndex and the synchronous Elasticsearch client revolves around the execution context—specifically, the mix-up between synchronous and asynchronous operations. LlamaIndex is designed to support both synchronous and asynchronous operations, which gives you the flexibility to choose the approach that best fits your application's architecture. However, this flexibility also requires careful management of execution contexts to avoid errors. From the error message you shared, it appears that there's an attempt to use an asynchronous operation in a context that expects synchronous execution. This is evident from the To resolve this issue, you should ensure that your usage of Elasticsearch operations through LlamaIndex aligns with the execution model of your Flask application. If your Flask application is designed to operate synchronously, you should use LlamaIndex's synchronous methods (e.g., Here's a quick recap of the approach you should take:
This distinction is crucial for avoiding errors related to execution context mismatches. If your application primarily uses Flask in a synchronous manner and you wish to keep it that way, focusing on the synchronous methods provided by LlamaIndex and ensuring any asynchronous operations are properly adapted to fit this model will be key. Remember, the goal is to match your operations with the execution context of your application. Misalignments between synchronous and asynchronous contexts are common sources of errors when integrating asynchronous capabilities into traditionally synchronous frameworks like Flask.
|
Every time you use es_vector_store, just recreate the object, don't get it from the context. |
Bug Description
This is ElasticsearchStore code:
This is restful api code:
And I got this error below:
How can I solve this?
Version
0.10.19
Steps to Reproduce
Start a Flask service, then use a REST request to query an index bound to Elasticsearch.
Relevant Logs/Tracbacks
No response
The text was updated successfully, but these errors were encountered: