0

I need to make output and input work at the same time. Already tried to make it several times, watched like 10 the same posts but they don't solve my problem at all.

tried solving by using prompt_toolkit library more solutions i tried to use: Python async: Waiting for stdin input while doing other stuff https://www.reddit.com/r/learnpython/comments/oqn18z/comment/h6cnpti/?utm_source=share&utm_medium=web2x&context=3

I saw people saying that better creating gui than terminal interface, but I need to make commands and don't know how to make terminal gui in like pyqt, and I think my stuff looks better in console than gui.

Also, i saw this post Problem with ainput (Async input in Python) in category "Review questions already on Stack Overflow to see if your question is a duplicate" and saw something in it, but I don't see how to use it properly or if its any helpful.

Example code:

user = User("...")
 
@user.on.message(text="ping")
async def messagehandler(msg: Message):
    msg.answer("pong")
 
async def main():
    while True:
        command = input(Style.RESET_ALL + " > ")
        await HandleCommand(command)
 
user.loop.run_until_complete(main())
user.run_forever()
FrostX
  • 23
  • 3

1 Answers1

0

So. I already knew the error was in while true, so i found a solution on how to make it async in this post: Asyncio, await and infinite loops

user = User("...")

@user.on.message(text="testbot")
async def messagehandler(msg: Message):
    print(msg.from_id) # author user id
    #  ^ 1 test - 661010401 | WORKS!

async def console():
    while True:
        await HandleCommand(await ainput(Style.RESET_ALL + " > "))

async def main():
    task = asyncio.Task(console())
    # let script some thime to work:
    await asyncio.sleep(3)
    # cancel task to avoid warning:
    task.cancel()
    with suppress(asyncio.CancelledError):
        await task  # await for task cancellation

user.loop.run_until_complete(main())
user.run_forever()

But now there's a different error, the commands handler is broken and only answers to the first command:

 > test

 test func work

 > cmds

 (no more inputs & prints)

If anyone wonders, the library is vkbottle for bot. (or user-bot)

FrostX
  • 23
  • 3
  • 1
    now the question is how to fix this thing. – FrostX Aug 24 '23 at 12:36
  • turns out this doesn't fix the problem and just deleting input to make event works after input doesn't work, if you change sleep time in main function to `float("inf")` the events still wont work. – FrostX Aug 24 '23 at 12:47