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

inquirer.select breaks asyncio operations #71

Open
sxpmoore opened this issue Jan 9, 2024 · 1 comment
Open

inquirer.select breaks asyncio operations #71

sxpmoore opened this issue Jan 9, 2024 · 1 comment

Comments

@sxpmoore
Copy link

sxpmoore commented Jan 9, 2024

I ran into a problem where calling this.

selection = inquirer.select(message="test this", choices=[Choice("opt1", name="for opt 1"), Choice("opt 2", name="for opt 2")], cycle=False).execute()

After calling that select, all asyncio commands would fail. Here is a demo to reproduce the issue. The 1st 2 pinghosts call works but not after the inquirer.select

import asyncio
from asyncio.subprocess import PIPE, STDOUT
from InquirerPy import inquirer
from InquirerPy.base.control import Choice


async def run_command(*args, host=None):
        process = await asyncio.create_subprocess_exec(*args, stdout=PIPE, stderr=PIPE)
        stdout, stderr = await process.communicate()
        return host, process.returncode == 0

def pinghosts(hosts, verbose=False):  
    jobs = (run_command(*((f'ping -c1 -W1 {host}').split()), host=host) for host in hosts)    
    results = asyncio.get_event_loop().run_until_complete( asyncio.gather( *jobs ) )
    return results


print( pinghosts(['box20', 'box1d2', 'box21']) )
print( pinghosts(['box20', 'box1d2', 'box21']) )

selection = inquirer.select(message="test this", choices=[Choice("opt1", name="for opt 1"), Choice("opt 2", name="for opt 2")], cycle=False).execute()

print( pinghosts(['box20', 'box1d2', 'box21']) )

it fails with this

Traceback (most recent call last):
File "./demo.py", line 25, in
print( pinghosts(['box20', 'box1d2', 'box21']) )
File "./demo.py", line 16, in pinghosts
results = asyncio.get_event_loop().run_until_complete( asyncio.gather( *jobs ) )
File "/usr/lib64/python3.8/asyncio/events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'MainThread'.

Is this my bug or a bug in how inquirer.select().exeute() operates ?

@sxpmoore
Copy link
Author

sxpmoore commented Jan 9, 2024

I found a workaround to open a new event loop.

def pinghosts(hosts): 
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    jobs = (run_command(*((f'ping -c1 -W1 {host}').split()), host=host) for host in hosts)    
    results = loop.run_until_complete( asyncio.gather( *jobs ) )
    return results

However, I'm not confident of this workaround. It would be better if inquirer call would leave the event loop running unless it created its own event loop.

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

No branches or pull requests

1 participant