Home>

Function with queue(python) not executing While True. I mean, I cannot write 5 messages to the queue in 1 second to check the addition to the same queue

from asyncio import Queue
import asyncio
q= Queue()
async def mama(q):
    while not q.empty():
        try:
            b= await q.get()
            await asyncio.sleep(5)
            print(b)
        except q.empty():
            None
asyncdef yo():
    while True:
        m= input("Enter: ")
        await q put(m)
        await mama(q)
asyncio.run(yo())
  • Answer # 1

    As I understand it, you want to enter data, send it to the queue, and in parallel, once every 5 seconds, pull it out of this queue and print it. Then you don't need to callmotherfromyo, and run both coroutines via .

    Working variant:

    from asyncio import Queue
    import asyncio
    q= Queue()
    async def mama(q):
        # At startup, the queue is empty, the not q.empty() condition is false, because of this, it won’t even go into the loop, change it to True
        while True:
            b= await q.get()
            await asyncio.sleep(5)
            print(b)
    asyncdef yo():
        while True:
            m= input("Enter: ")
            await q put(m)
            # We add a pause so that the second coroutine has time to get control and pull the value out of the queue
            await asyncio.sleep(0.1)
    async def runner():
        # Run two coroutines at the same time
        await asyncio.gather(mama(q), yo())
    asyncio.get_event_loop().run_until_complete(runner())
    

    I/O example:

    Enter: 1
    Enter: 2
    Enter: 3
    Enter: 4
    Enter: 5
    Enter: 6
    Enter: 7
    Enter: 8
    Enter: 9
    one
    Enter: 10
    Enter: 11
    Enter: 12
    Enter: 13
    2
    Enter: 14
    Enter: 15
    Enter:
    ...
    

    Keep in mind thatinput()is a synchronous function, and during its execution, asynchronous functions in the current thread will not work, i.e. if nothing is entered, the queue will not be processed.

  • Answer # 2

    As I understand it, you want to enter data, send it to the queue, and in parallel, once every 5 seconds, pull it out of this queue and print it. Then you don't need to callmotherfromyo, and run both coroutines via .

    Working variant:

    from asyncio import Queue
    import asyncio
    q= Queue()
    async def mama(q):
        # At startup, the queue is empty, the not q.empty() condition is false, because of this, it won’t even go into the loop, change it to True
        while True:
            b= await q.get()
            await asyncio.sleep(5)
            print(b)
    asyncdef yo():
        while True:
            m= input("Enter: ")
            await q put(m)
            # We add a pause so that the second coroutine has time to get control and pull the value out of the queue
            await asyncio.sleep(0.1)
    async def runner():
        # Run two coroutines at the same time
        await asyncio.gather(mama(q), yo())
    asyncio.get_event_loop().run_until_complete(runner())
    

    I/O example:

    Enter: 1
    Enter: 2
    Enter: 3
    Enter: 4
    Enter: 5
    Enter: 6
    Enter: 7
    Enter: 8
    Enter: 9
    one
    Enter: 10
    Enter: 11
    Enter: 12
    Enter: 13
    2
    Enter: 14
    Enter: 15
    Enter:
    ...
    

    Keep in mind thatinput()is a synchronous function, and during its execution, asynchronous functions in the current thread will not work, i.e. if nothing is entered, the queue will not be processed.