How to cancel task?
Hello, I tried to cancel tasks which are not current and tried the following code. My expectation was, that the task will wait until the previous task is finished and if it is not current anymore, then it is canceled (From the docs: The task object will execute the function only once per virtual kernel and will only store one result per virtual kernel. When called multiple times, the previously started thread or asyncio task result will be ignored.)
So my expectation was, that if I click 10-times the button when running the task for the first time, some of the tasks will be canceled and the total number of runs will be smaller than the number of clicks. What I am doing wrong?
The code is shared as gist, since it does not run in pycafe, (however it works fine on local PC) and the URL generated by pycafe is too long
https://gist.github.com/robert-marik/f41782f83e361636677099808cf1621a
.
Thank you.
Gist
Solara, task cancel attempt
Solara, task cancel attempt. GitHub Gist: instantly share code, notes, and snippets.
2 Replies
Hi,
You don't need to cancel yourself, you are being cancelled if the a new task is started. You also check the is_current before sleeping, so it would run immediately. This version might be more what you wanted (although an async version that runs on pycafe):
https://py.cafe/maartenbreddels/solara-cancel-task
PyCafe - Solara - Solara Interactive Data Fetcher
Create & Share Streamlit, Dash and Python Apps Online.
Thank you.