#πŸ”’ Weird traceback

15 messages Β· Page 1 of 1 (latest)

neat cliff
#

async def get_user_input():
    try:

        name = await asyncio.wait_for(input("Type your name: "), timeout=5)
        key = await asyncio.wait_for(input("Write your key: "), timeout=5)
        return name, key
    except asyncio.TimeoutError:
        print("Your time is out.")
        return None, None


async def generate_palindromes():
    while True:
        s = ''.join(random.choices(string.ascii_lowercase, k=5))
        palindrome = s + s[::-1]
        print(f"{palindrome} created")
        await asyncio.sleep(1)


async def main():
    task1 = asyncio.create_task(get_user_input())
    task2 = asyncio.create_task(generate_palindromes())

    name, key = await task1
    if name and key:
        print(f"Name: {name}, Key: {key}")

    task2.cancel()
    try:
        await task2
    except asyncio.CancelledError:
        print("Generation of palindrome cancelled.")


asyncio.run(main())```
untold girderBOT
#

@neat cliff

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

livid kernel
#

... where is the traceback?

neat cliff
#
TypeError: An asyncio.Future, a coroutine or an awaitable is required

neat cliff
#
  File "/home/user/Python/foo.py", line 41, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/user/Python/foo.py", line 30, in main
    name, key = await task1
  File "/home/user/Python/foo.py", line 10, in get_user_input
    name = await asyncio.wait_for(input("Type your name: "), timeout=5)
  File "/usr/lib/python3.10/asyncio/tasks.py", line 426, in wait_for
    fut = ensure_future(fut, loop=loop)
  File "/usr/lib/python3.10/asyncio/tasks.py", line 615, in ensure_future
    return _ensure_future(coro_or_future, loop=loop)
  File "/usr/lib/python3.10/asyncio/tasks.py", line 630, in _ensure_future
    raise TypeError('An asyncio.Future, a coroutine or an awaitable '
TypeError: An asyncio.Future, a coroutine or an awaitable is required
dark anvil
#

!d asyncio.wait_for

untold girderBOT
#

coroutine asyncio.wait_for(aw, timeout)```
Wait for the *aw* [awaitable](https://docs.python.org/3/library/asyncio-task.html#asyncio-awaitables) to complete with a timeout.

If *aw* is a coroutine it is automatically scheduled as a Task.

*timeout* can either be `None` or a float or int number of seconds to wait for. If *timeout* is `None`, block until the future completes.

If a timeout occurs, it cancels the task and raises [`TimeoutError`](https://docs.python.org/3/library/exceptions.html#TimeoutError).

To avoid the task [`cancellation`](https://docs.python.org/3/library/asyncio-task.html#asyncio.Task.cancel), wrap it in [`shield()`](https://docs.python.org/3/library/asyncio-task.html#asyncio.shield).

The function will wait until the future is actually cancelled, so the total wait time may exceed the *timeout*. If an exception happens during cancellation, it is propagated...
inner nebula
#

Timing out user input is a real pain in python sorry about that

#

You might be able to get it working with something that controls the terminal completely, eg textual

dark anvil
#

I think I handled the previously with threads that I terminated after a timeout, but ya, it was a pain and messy.

inner nebula
#

Can't terminate a thread running input

untold girderBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.