Skip to content

Conversation

@Margravegit
Copy link

Added fullstop behavior

Added fullstop behavior
@jazzz
Copy link
Owner

jazzz commented Mar 15, 2018

Looking Good!


async def update(self):
while True:
await asyncio.sleep(0.1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this works great, with co-routines we can simplify things by removing the conditional logic. In a coroutine when you 'await' you are suspending the function, so it resumes where it left off.

async def update():
    n = 255
    while True:
        for x in range(n+1):
            await asyncio.sleep(0.1)
            self.color = Color(x,0,0)
        for x in range(n-1,0,-1):
            await asyncio.sleep(0.1)
            self.color = Color(x,0,0)

Better yet we could also do something like if you wanted to reuse the functionality.

async def update():
    async for x in thereAndBackIncrementor(0,10):
        self.color = Color(x,0,0)
        await asyncio.sleep(0.1)


async def thereAndBackIncrementor(firstVal,lastVal):
    for x in range(lastVal + 1):
        yield x
    for x in range(lastVal - 1, firstVal, -1):
        yield x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants