0

I have two coroutines one of which is using aioschedule. This is my code

import aioschedule as schedule
import asyncio

async def foo():
    while True:
        print('foooooo')
        await asyncio.sleep(5)

async def bar():
    while True:
        print('bar')
        await asyncio.sleep(1)

schedule.every(2).seconds.do(bar)

loop = asyncio.get_event_loop()
loop.create_task(schedule.run_pending())
loop.create_task(foo())

try:
    loop.run_forever()
except KeyboardInterrupt:
    loop.stop()

What i want is it should printed bar every n seconds when other task is running but the output is only foooooo. Am i missing something?

Arief G
  • 33
  • 7

1 Answers1

0

try this:

   import aioschedule as schedule
   import asyncio

   async def foo():
       while True:
           print('foooooo')
           await asyncio.sleep(5)

    async def bar():
        while True:
            print('bar')
            await asyncio.sleep(1)

    #schedule.every(2).seconds.do(bar) <---removed line

    loop = asyncio.get_event_loop()
    loop.create_task(schedule.run_pending())
    loop.create_task(foo())
    loop.create_task(bar()) #<---- added line

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        loop.stop()
Regpa
  • 1
  • 1
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you edit your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Skully Dec 15 '22 at 13:11