mode
mode copied to clipboard
CancelledError does not cancel 'mode.Worker'
Checklist
- [x] I have included information about relevant versions
- [x] I have verified that the issue persists when using the
master
branch of Mode.
Steps to reproduce
Have a Service
with a canceled background task wrapped into Worker
:
class Api(mode.Service):
@mode.Service.task
def main(self) -> None:
raise asyncio.CancelledError()
if __name__ == '__main__':
mode.Worker(Api()).execute_from_command_line()
Expected behavior
A task's CancelledError
can be propagated to the worker.
Actual behavior
An application seems stuck and unresponsive.
Full traceback
Paste the full traceback (if there is any)
Versions
- Python version: 3.7.5
- Mode version: 4.1.2
- Operating system: Debian-based official Python 3.7 image
Also, is there a way to stop the worker upon request? Right now, I traverse through a beacon to find the worker and schedule its shutdown, but is there a better way?
asyncio.ensure_future(self.beacon.root.data.stop())
Thanks!
The program above raising CancelledError works for me, it pretty much just exits (mode master)
I agree using the beacon to stop is not very great, I guess the best way is if you have a reference to the worker already.
You can stop the worker, but you can also stop the service passed to Worker()
: that's what we do in Faust. We have an App
class that starts all the services needed, and call app.stop()
to stop the worker (the app is also passed pretty much everywhere, if you don't like passing you could use dependency injection or a Local (mode.locals).
Also... there's always raise SystemExit()
:)