Python - about asyncio

Asked

Viewed 57 times

0

I started testing pynput with asyncio, but there’s one problem I can’t solve at all. My code:

current = set()


def on_press(key):
    if key == keyboard.Key.up:
        current.add(key)
        print('Y')
    if key == keyboard.Key.down:
        print('Z')


async def listener():
    with keyboard.Listener(on_release=None, on_press=on_press) as listener:
        listener.join()


loop = asyncio.get_event_loop()
loop.run_until_complete(listener())

print('x')

Well, from what I understand, even what comes after loop.run_until_complete(função)"does not depend on the end of the loop, so the print('x') should run along with the loop, that’s it?

In my example, nothing written after the loop runs until it runs out, what I think is that my problem is here:

async def listener():
with keyboard.Listener(on_release=None, on_press=on_press) as listener:
    listener.join()

But still I don’t know how to fix.

  • Are you sure the library pynput use coroutines? From what I read quickly in the documentation, use thread.

  • My God, I was so focused thinking that my problem was with the asyncio that I didn’t stop to think about the pynput, thanks for the help.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.