Search code examples
pythonpython-3.xevent-loopurwid

How can I create an asynchronous program with urwid and asyncio?


I want to build a chatroom with aiortc. Frist of all, I want to build a mockup with urwid as cli and asyncio. The urwid part is already running fine, user input is possible. I know want to run a coroutine that generates random text and works as chat clients texting in that chatroom.

I have tried to run my urwid function with the mainloop as an asyncio coroutine but without success. I don't know how to integrate an asynchronous function into my urwid mainloop.

def unhandled(key):
    """
    functin to handle input
    """
    global TEXT_INPUT
    global lw_user_input
    global lw_chatroom
    global listbox_chatroom

    if not isinstance(key, tuple):
        if key == 'enter':
            del lw_user_input[-1]
            # create widegt and fill with user input
            lw_chatroom.append(widget)
            TEXT_INPUT = ""
            listbox_chatroom.set_focus(len(lw_chatroom)-1, 'above')

        elif key == 'esc':
            raise urwid.ExitMainLoop()
        elif key == 'backspace':
            if len(lw_user_input) > 0:
                user_input = lw_user_input[0].get_text()[0]
                user_input = user_input[:-1]
                del lw_user_input[-1]
                TEXT_INPUT = user_input
                lw_user_input.append(urwid.Text(TEXT_INPUT))
        else:
            TEXT_INPUT += key  # repr(key)
            if len(lw_user_input) > 0:
                del lw_user_input[-1]
                lw_user_input.append(urwid.Text(TEXT_INPUT))
            else:
                lw_user_input.append(urwid.Text(key))



def generate_output():
    global lw_chatroom
    global listbox_chatroom
    while True:
        # generate text and widgets and post with delay
        lw_chatroom.append(chat_widget)
        listbox_chatroom.set_focus(len(lw_chatroom)-1, 'above')


def create_cli():
    # generate all widgets
    uloop = urwid.MainLoop(frame, palette, screen,
                           unhandled_input=unhandled)
    uloop.start()


if __name__ == '__main__':
    create_cli()

I want to run generate_output() and unhandled(key) asynchronously. I have no idea how to.


Solution

  • Ok, I figured it out.

    It is as simple as this:

    aloop = asyncio.get_event_loop()
    
    ev_loop = urwid.AsyncioEventLoop(loop=aloop)
    loop = urwid.MainLoop(frame, palette, screen,
                          unhandled_input=unhandled, event_loop=ev_loop)
    aloop.create_task(generate_output())
    loop.run()