xiaoguoNo1
xiaoguoNo1

Reputation: 11

Handle python coroutines in with a game tick without any event loop libs

What i need is a async sleep function doesn't block and easy to use. The sleep duration should be divided into smaller intervals (ticks), with the function continuing to "sleep" until a specified number of ticks are completed. The number of ticks is sleep duration divided by the tick interval, and the tick function is element of game framework.

import time
# do not import any other libs, complete my functions

def tick():
    # do something, maybe resume corotines or other awaitable
    pass

async def sleep(seconds):
    # wait until time goes on for seconds
    pass

async def test():
    print("start")
    await sleep(2)
    print("end")

# call test somewhere

# simulate game tick
while True:
    tick()
    time.sleep(0.01)

I have read the doc but most example use asyncio. i tried to yield in sleep function to pause it, but raise error "object async_generator can't be used in 'await' expression".

Upvotes: 1

Views: 28

Answers (0)

Related Questions