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

nest_asyncio causes failure to find empty port #8730

Closed
1 task done
aymeric-roucher opened this issue Jul 9, 2024 · 7 comments
Closed
1 task done

nest_asyncio causes failure to find empty port #8730

aymeric-roucher opened this issue Jul 9, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@aymeric-roucher
Copy link

Describe the bug

import gradio as gr
import nest_asyncio
nest_asyncio.apply()

with gr.Blocks() as demo: 
    gr.Markdown("### Ok")

if __name__ == "__main__":
    demo.launch()

Creates error:

OSError: Cannot find empty port in range: 7860-7860. You can specify a different port by setting the GRADIO_SERVER_PORT environment variable or passing the `server_port` parameter to `launch()`.

The error happens with 4.29.0, 4.33.0, and 4.37.2.

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import gradio as gr
import nest_asyncio
nest_asyncio.apply()

with gr.Blocks() as demo: 
    gr.Markdown("### Ok")

if __name__ == "__main__":
    demo.launch()

Screenshot

No response

Logs

No response

System Info

sdk_version: 4.37.2

Severity

Blocking usage of gradio

@aymeric-roucher aymeric-roucher added the bug Something isn't working label Jul 9, 2024
@aymeric-roucher
Copy link
Author

This error happens in Spaces, on a local setup I get RuntimeError: There is no current event loop in thread 'Thread-14 (run)'. instead.

@abidlabs
Copy link
Member

abidlabs commented Jul 10, 2024

Thanks @aymeric-roucher btw why do you need nest_asyncio on Spaces? I think nest_asyncio's event loop is interfering with Gradio's ability to find an open port

@aymeric-roucher
Copy link
Author

I used it to run parallel API calls @abidlabs .
The exact same code in my Space used to work, but I don't remember the Gradio version with which it worked.

If there's a recommended alternative for parallel API calls I'd be glad to use it!

@abidlabs
Copy link
Member

Thanks for the clarification, @aymeric-roucher! To avoid conflicts with Gradio's event loop, you could consider using the asyncio.gather() function for parallel API calls without nest_asyncio or use other libraries like aiohttp for asynchronous HTTP requests, which can help with parallelism in a more compatible way. If you have an example code snippet, happy to suggest something specific to your use case

@abidlabs
Copy link
Member

But I also have another idea to fix this, let me try something

@abidlabs
Copy link
Member

Ok sadly my fix does not work. Basically what's happening is that next_asyncio actually patches the asyncio library. From its documentation:

By design asyncio does not allow its event loop to be nested. This presents a practical problem: When in an environment where the event loop is already running it’s impossible to run tasks and wait for the result. Trying to do so will give the error “RuntimeError: This event loop is already running”.

The issue pops up in various environments, such as web servers, GUI applications and in Jupyter notebooks.

This module patches asyncio to allow nested use of asyncio.run and loop.run_until_complete.

This means that our use of asyncio is affected. We could try to patch the patch but that seems like a bad idea 😅

So my suggestion is that basically you should avoid using this library. Unless you are running in a jupyter notebook or some other weird environment where you always have an event loop running, you shouldn't need it. I'll close this issue but happy to debug with you in this thread or privately.

@abidlabs abidlabs closed this as not planned Won't fix, can't repro, duplicate, stale Jul 10, 2024
@aymeric-roucher
Copy link
Author

THank you @abidlabs for looking into this! It's strange to think it was working before but does not anymore, that's why I thought it could be solved. I will try remaking my space to avoid using the library altogether!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants