[issue32104] add method throw() to asyncio.Task
New submission from Oleg K : currently there is no other way to interrupt task but to call cancel() which will: "This arranges for a CancelledError to be thrown into the wrapped coroutine on the next cycle through the event loop." in order to write advanced Tasks there should be a way to throw custom exceptions into active Task. so it can react accordingly. i am looking for the same thing as "generator.throw()" is providing, allowing to interrupt generators different way. -- components: asyncio messages: 306644 nosy: Oleg K2, yselivanov priority: normal severity: normal status: open title: add method throw() to asyncio.Task type: enhancement versions: Python 3.6 ___ Python tracker <https://bugs.python.org/issue32104> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue32104] add method throw() to asyncio.Task
Oleg K added the comment: What is an "advanced" task? Why CancelledError is not enough? What's the actual use case? -- for instance, i want to create a task which is responsible for controlling a "slow resource" that should accessed carefully not to DOS it, that could be an long running api call, or report generation procedure, or slow sqlite query. that task may be interrupted when program needs to respond quickly for example UI is requesting another action with that slow resource, that should happen asap. if i could throw custom exception, that task would suspend or interrupt current action, then execute something new and urgent, and then continue where it left of. - also there is another case i have in mind, there is a task which monitors video stream with different means to see if that stream is alive an fine, it uses external calls to ffmpeg to check how the stream is doing, - that monitoring procedure is continuous always running task, sometimes there is a network problem or one of CDN servers go down, when that event is happening i need to throw custom exception to that "monitoring task" no matter at which stage it is at. when that task will get custom exception it will not exit but will switch to different operation mode, it should not exit because the monitoring is continuous process , it just hast to interpret its measurements differently, and use different ways to measure video state. --- I hope i made clear examples, lmk if you need more details. thanks! -- ___ Python tracker <https://bugs.python.org/issue32104> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue32181] runaway Tasks with Task.cancel() ignored.
New submission from Oleg K : (tested in VM and in real linux) there is an issue with Task, in some cases task will ignore cancellation and will keep running for a while. there is some explanation needed regarding the python_task_cancel.py example. 1)there is a async Integer Generator (my_generator) it has sleep() inside, and produces integers. 2)there is async coro test(), which is consumer of integers , it has several while True: consume = yield 3) there are tasks "a_task()", the job task is doing: it reads from generator, and pushes integer to active "async coro test()" my_generator -iterated by-> a_task() -sends Int to-> test() 4) "async coro test()" is protected by lock, so only one task may push value to it at a time. 5) "task_context" is a context manager, a helper which starts tasks and cancels them __aenter__ -> loop.create_task(self.task) __aexit__ -> self.running_task.cancel() here is how is it used async with task_context( async-coro-to-be-started-as-a-task ): # task is created while True: consume = yield if consume > 10 :break # task is CANCELLED -- The Case: the issue is there when there is sequential aenters and aexits async with task_context( async-coro-to-be-started-as-a-task ): # task #1 is created while True: consume = yield if consume > 10 :break # task #1 is CANCELLED async with task_context( async-coro-to-be-started-as-a-task ): # task #2 is created while True: consume = yield if consume > 20 :break # task #2 is CANCELLED async with task_context( async-coro-to-be-started-as-a-task ): # task #3 is created while True: consume = yield if consume > 10 :break # task #3 is CANCELLED what may go wrong here? - at the end there will be 3 live task reading from same generator! 2 tasks have clearly got cancel() call but, according to output they still there and working: there is part of output as a proof: my_generator: [0.3] PRODUCE VALUE 28 send done: 20 task id= 1 will send : 28 task id= 4 consume 3 28 my_generator: [0.3] PRODUCE VALUE 29 send done: 28 task id= 4 will send : 29 task id= 2 consume 3 29 send done: 29 task id= 2 my_generator: [0.4] PRODUCE VALUE 21 will send : 21 task id= 1 consume 3 21 send done: 21 task id= 1 my_generator: [0.3] PRODUCE VALUE 30 will send : 30 task id= 3 consume 3 30 send done: 30 task id= 3 my_generator: [0.3] PRODUCE VALUE 31 will send : 31 task id= 3 "task id= 3" "task id= 2" "task id= 4" that means that all tasks are there!, which should not happen. but, these tasks WILL GET cancellation call execute just after "async coro test()" will exit, that exit somehow will trigger pending exit of tasks, which should have happened long ago!. also, there is no reliable way to wait task termination, which is also major issue with real life asyncio usage, i need to have a mean to wait for task to complete wait until its "finally:" is done and all resources are free. -- components: asyncio files: python_task_cancel.py messages: 307307 nosy: Oleg K2, yselivanov priority: normal severity: normal status: open title: runaway Tasks with Task.cancel() ignored. type: behavior versions: Python 3.6 Added file: https://bugs.python.org/file47307/python_task_cancel.py ___ Python tracker <https://bugs.python.org/issue32181> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com