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

RuntimeError: This event loop is already running #30

Closed
corzani opened this issue Nov 25, 2021 · 5 comments
Closed

RuntimeError: This event loop is already running #30

corzani opened this issue Nov 25, 2021 · 5 comments
Labels
enhancement New feature or request

Comments

@corzani
Copy link

corzani commented Nov 25, 2021

I am dealing with InquirerPy within an asyncio task something like:

import asyncio
from InquirerPy import inquirer

async def main():
    # Await... Something async...
    
    selected = inquirer.fuzzy(
        message="Select or Type a genre:",
        choices=['a', 'b', 'c']
    ).execute()

    print(selected)

if __name__ == '__main__':
    asyncio.run(main())

In any case I receive this kind of error:

    selected = await inquirer.fuzzy(
  File "/usr/local/lib/python3.9/site-packages/InquirerPy/base/simple.py", line 321, in execute
    result = self._run()
  File "/usr/local/lib/python3.9/site-packages/InquirerPy/base/complex.py", line 205, in _run
    return self.application.run()
  File "/usr/local/lib/python3.9/site-packages/prompt_toolkit/application/application.py", line 890, in run
    return loop.run_until_complete(
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 618, in run_until_complete
    self._check_running()
  File "/usr/local/Cellar/python@3.9/3.9.8/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 578, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
sys:1: RuntimeWarning: coroutine 'Application.run_async' was never awaited

Is there a better way to avoid this error instead of using nest-asyncio like libraries?

@kazhala
Copy link
Owner

kazhala commented Nov 25, 2021

Hi @corzani ,

Thanks for reporting. I'm aware of this issue at the moment. Async application is not yet officially supported yet. It is in my backlog to be supported in the near future.

Stay tuned. I'll work on this possibly after the next release.

Thanks,
Kevin

@kazhala kazhala added the enhancement New feature or request label Nov 25, 2021
@kazhala
Copy link
Owner

kazhala commented Nov 25, 2021

Sorry forgot to answer your last question. Although not well tested, but for now you can run the app async like this.

import asyncio
from InquirerPy import inquirer

async def main():
    prompt = inquirer.fuzzy(
        message="Select actions:",
        choices=["hello", "weather", "what", "whoa", "hey", "yo"],
        default="he",
    )
    result = await prompt.application.run_async()


if __name__ == "__main__":
    asyncio.run(main())

@corzani
Copy link
Author

corzani commented Nov 25, 2021

Thank you @kazhala
This solution works and at the moment I'll stick with it.

@kazhala
Copy link
Owner

kazhala commented Dec 9, 2021

Async execution will be included in the next release.

Forgot to mention that for now, if you wish to run async for the following prompt: text, filepath, confirm, secret, you'll do the following:

import asyncio
from InquirerPy import inquirer

async def main():
    prompt = inquirer.confirm(message="Select actions:")
    result = await prompt._session.prompt_async()

if __name__ == "__main__":
    asyncio.run(main())

Internally, these are built differently than list prompts such as fuzzy.

Thanks,
Kevin

@corzani
Copy link
Author

corzani commented Dec 9, 2021

@kazhala
Thank you Kevin, that's very kind of you. I only needed the fuzzy bit and at the moment everything works well (at least at your side, the VLC python wrapper is very bad but I can't blame you for that :D ).
I made a poor man command line radio because I was bored of life that day... It's called radio81.
You can try it and feel, in some ways, the father of that radio too :). Unfortunately that version requires VLC but I guess you have it already...

pipx install radio81

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants