haystack
haystack copied to clipboard
AsyncPipeline.run does not work in a notebook environment
Describe the bug
When using AsyncPipeline.run we get a RunTimeError because it calls asyncio.run internally which fails because Jupyter is already running an event-loop.
Error message
RuntimeError: asyncio.run() cannot be called from a running event loop
Expected behavior The method runs in a notebook environment.
Additional context We can fix it by doing something like this:
try:
# Check if there's already a running event loop
loop = asyncio.get_running_loop()
# If we're here, there's a running loop, so use it
return loop.run_until_complete(self.run_async(data))
except RuntimeError:
# No running event loop, create a new one with asyncio.run()
return asyncio.run(self.run_async(data))
To Reproduce Steps to reproduce the behavior
FAQ Check
- [ ] Have you had a look at our new FAQ page?
System:
- OS:
- GPU/CPU:
- Haystack version (commit or version number):
- DocumentStore:
- Reader:
- Retriever: