PythonistaGuild / TwitchIO

An Async Bot/API wrapper for Twitch made in Python.
https://twitchio.dev
MIT License
798 stars 162 forks source link

choose specific time to sleep when using decorator `routines.routine` #323

Closed ccppoo closed 2 years ago

ccppoo commented 2 years ago

Current explanation about wait_first in doc is ambiguous.

https://github.com/TwitchIO/TwitchIO/blob/14b0880d9c22f6fc599f23ddbcb08e1aa8c962c4/twitchio/ext/routines/__init__.py#L364-L366

It says it waits for specific time so, this could be understood as it's requiring int or float.

https://github.com/TwitchIO/TwitchIO/blob/14b0880d9c22f6fc599f23ddbcb08e1aa8c962c4/twitchio/ext/routines/__init__.py#L390-L407

But it actually waits for time to wait before the next iteration of the routine. ↑

https://github.com/TwitchIO/TwitchIO/blob/14b0880d9c22f6fc599f23ddbcb08e1aa8c962c4/twitchio/ext/routines/__init__.py#L288-L289

delta passed to Routine is used wait for next iteration as the code shows ↑


proposal

add wait_for keyword parameter requiring int or float to wait for specific time

@routines.routine(seconds=600,wait_for=20)
async def do_this_job_every_20_sec(self):
    await do_something( ... )
    ...

for example wait_for key word will be used for jobs that has time out like DB connections

that should run first in the place when the app started and then run after every 10 minutes

github-actions[bot] commented 2 years ago

Hello! Thanks for the issue. If this is a general help question, for a faster response consider joining the official Discord Server

Else if you have an issue with the library please wait for someone to help you here.

chillymosh commented 2 years ago

The type hint shows if it takes a value it has to be a bool so it is not ambiguous as to whether it could be an int or a float. The argument name of wait_first implies the question "Would you like to wait first before the first iteration", where specified time is the routine loop interval. The docs can be updated to reflect this better.

The wait_for proposal is basically the already existing @before.routine method, where you can simply perform a task and then asyncio.sleep() before the main body executes.

ccppoo commented 2 years ago

@chillymosh I haven't thought using @before.routine thanks ,my bad

ccppoo commented 2 years ago

Just for someone else like me Untitled Diagram drawio(2)

chillymosh commented 2 years ago

Closing as not an issue.