Run events every given number of seconds#2512
Conversation
|
All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-2512-all-demos |
|
So cool! |
| return continuous_fn | ||
|
|
||
|
|
||
| async def cancel_tasks(task_ids: List[str]): |
There was a problem hiding this comment.
Refactoring some of the code from the cancels PR so that it's easier to use in other parts of the codebase
| api_key: Optional[str] = None, | ||
| alias: Optional[str] = None, | ||
| _js: Optional[str] = None, | ||
| every: None | int = None, |
There was a problem hiding this comment.
Unrelated to your PR, this event trigger is missing many arguments, such as batch. It would be good to add if possible (or we can create a separate issue for it)
There was a problem hiding this comment.
I'll do that in a separate PR! Thank you for pointing this out!
| batch: bool = False, | ||
| max_batch_size: int = 4, | ||
| cancels: List[int] | None = None, | ||
| every: int | None = None, |
There was a problem hiding this comment.
| every: int | None = None, | |
| every: float | None = None, |
| def get_continuous_fn(fn, every): | ||
| def continuous_fn(*args): | ||
| called_before = False | ||
| while True: | ||
| output = fn(*args) | ||
| if called_before: | ||
| time.sleep(every) | ||
| else: | ||
| called_before = True | ||
| yield output | ||
|
|
||
| return continuous_fn |
There was a problem hiding this comment.
| def get_continuous_fn(fn, every): | |
| def continuous_fn(*args): | |
| called_before = False | |
| while True: | |
| output = fn(*args) | |
| if called_before: | |
| time.sleep(every) | |
| else: | |
| called_before = True | |
| yield output | |
| return continuous_fn | |
| def get_continuous_fn(fn, every): | |
| def continuous_fn(*args): | |
| while True: | |
| output = fn(*args) | |
| yield output | |
| time.sleep(every) | |
| return continuous_fn |
Also, would it be better to use asyncio.sleep here instead of time.sleep so that the queue worker is free to do something else in the meantime?
There was a problem hiding this comment.
Ah I see so you later avoid putting such events in the queue altogether because they don't get completed. So I'm not sure how much of a difference it makes to use asyncio.sleep but I do think it's probably better than having a background thread just sit there doing nothing
There was a problem hiding this comment.
Lol love this refactor! Thank you.
Yea originally I had this as an async function but the problem with that is that you only want to sleep after the first function call. Tricky to implement without a generator and we don't currently support async generators. Will think about it though (may add async generators to #2552 )
|
This looks great @freddyaboulton! Tested it and works well, and plays nicely with One suggestion would be to add some more documentation about it (maybe in this Guide?) since this is a pretty big feature. I would also mention somewhere that if you define an event that runs Very excited about the possibilities this opens up! |
pngwn
left a comment
There was a problem hiding this comment.
lgtm! Awesome work @freddyaboulton!
Description
Adds the
everykey word argument to the events so that they runeverynumber of seconds.Fixes: #2050
Demo
Checklist:
A note about the CHANGELOG
Hello 👋 and thank you for contributing to Gradio!
All pull requests must update the change log located in CHANGELOG.md, unless the pull request is labeled with the "no-changelog-update" label.
Please add a brief summary of the change to the Upcoming Release > Full Changelog section of the CHANGELOG.md file and include
a link to the PR (formatted in markdown) and a link to your github profile (if you like). For example, "* Added a cool new feature by
[@myusername](link-to-your-github-profile)in[PR 11111](https://github.com/gradio-app/gradio/pull/11111)".If you would like to elaborate on your change further, feel free to include a longer explanation in the other sections.
If you would like an image/gif/video showcasing your feature, it may be best to edit the CHANGELOG file using the
GitHub web UI since that lets you upload files directly via drag-and-drop.