#[1]Index [2]Search [3]Deploying [4]Testing

[5]Channels ____________________ * [6]Introduction * [7]Installation * [8]Tutorial * [9]Consumers * [10]Routing * [11]Database Access * [12]Channel Layers * [13]Sessions * [14]Authentication * [15]Security * [16]Testing * [17]Worker and Background Tasks + [18]Sending + [19]Receiving and Consumers * [20]Deploying * [21]Troubleshooting

* [22]ASGI * [23]Channel Layer Specification * [24]Community Projects * [25]Contributing * [26]Support * [27]Release Notes

[28]Channels * * Worker and Background Tasks * [29]View page source __________________________________________________________________

Worker and Background Tasks[30]

While [31]channel layers are primarily designed for communicating between different instances of ASGI applications, they can also be used to offload work to a set of worker servers listening on fixed channel names, as a simple, very-low-latency task queue.

Note

The worker/background tasks system in Channels is simple and very fast, and achieves this by not having some features you may find useful, such as retries or return values.

We recommend you use it for work that does not need guarantees around being complete (at-most-once delivery), and for work that needs more guarantees, look into a separate dedicated task queue.

This feature does not work with the in-memory channel layer.

Setting up background tasks works in two parts - sending the events, and then setting up the consumers to receive and process the events.

Sending[32]

To send an event, just send it to a fixed channel name. For example, let’s say we want a background process that pre-caches thumbnails: # Inside a consumer self.channel_layer.send( "thumbnails-generate", { "type": "generate", "id": 123456789, }, )

Note that the event you send must have a type key, even if only one type of message is being sent over the channel, as it will turn into an event a consumer has to handle.

Also remember that if you are sending the event from a synchronous environment, you have to use the asgiref.sync.async_to_sync wrapper as specified in [33]channel layers.

Receiving and Consumers[34]

Channels will present incoming worker tasks to you as events inside a scope with a type of channel, and a channel key matching the channel name. We recommend you use ProtocolTypeRouter and ChannelNameRouter (see [35]Routing for more) to arrange your consumers: application = ProtocolTypeRouter({ ... "channel": ChannelNameRouter({ "thumbnails-generate": consumers.GenerateConsumer.as_asgi(), "thumbnails-delete": consumers.DeleteConsumer.as_asgi(), }), })

You’ll be specifying the type values of the individual events yourself when you send them, so decide what your names are going to be and write consumers to match. For example, here’s a basic consumer that expects to receive an event with type test.print, and a text value containing the text to print: class PrintConsumer(SyncConsumer): def test_print(self, message): print("Test: " + message["text"])

Once you’ve hooked up the consumers, all you need to do is run a process that will handle them. In lieu of a protocol server - as there are no connections involved here - Channels instead provides you this with the runworker command: python manage.py runworker thumbnails-generate thumbnails-delete

Note that runworker will only listen to the channels you pass it on the command line. If you do not include a channel, or forget to run the worker, your events will not be received and acted upon.

[36]Previous [37]Next __________________________________________________________________

© Copyright 2022, Django Software Foundation. Built with [38]Sphinx using a [39]theme provided by [40]Read the Docs.

References

Visible links: 1. https://channels.readthedocs.io/en/latest/genindex.html 2. https://channels.readthedocs.io/en/latest/search.html 3. https://channels.readthedocs.io/en/latest/deploying.html 4. https://channels.readthedocs.io/en/latest/topics/testing.html 5. https://channels.readthedocs.io/en/latest/index.html 6. https://channels.readthedocs.io/en/latest/introduction.html 7. https://channels.readthedocs.io/en/latest/installation.html 8. https://channels.readthedocs.io/en/latest/tutorial/index.html 9. https://channels.readthedocs.io/en/latest/topics/consumers.html 10. https://channels.readthedocs.io/en/latest/topics/routing.html 11. https://channels.readthedocs.io/en/latest/topics/databases.html 12. https://channels.readthedocs.io/en/latest/topics/channel_layers.html 13. https://channels.readthedocs.io/en/latest/topics/sessions.html 14. https://channels.readthedocs.io/en/latest/topics/authentication.html 15. https://channels.readthedocs.io/en/latest/topics/security.html 16. https://channels.readthedocs.io/en/latest/topics/testing.html 17. https://channels.readthedocs.io/en/latest/topics/worker.html 18. https://channels.readthedocs.io/en/latest/topics/worker.html#sending 19. https://channels.readthedocs.io/en/latest/topics/worker.html#receiving-and-consumers 20. https://channels.readthedocs.io/en/latest/deploying.html 21. https://channels.readthedocs.io/en/latest/topics/troubleshooting.html 22. https://channels.readthedocs.io/en/latest/asgi.html 23. https://channels.readthedocs.io/en/latest/channel_layer_spec.html 24. https://channels.readthedocs.io/en/latest/community.html 25. https://channels.readthedocs.io/en/latest/contributing.html 26. https://channels.readthedocs.io/en/latest/support.html 27. https://channels.readthedocs.io/en/latest/releases/index.html 28. https://channels.readthedocs.io/en/latest/index.html 29. https://channels.readthedocs.io/en/latest/_sources/topics/worker.rst.txt 30. https://channels.readthedocs.io/en/latest/topics/worker.html#worker-and-background-tasks 31. https://channels.readthedocs.io/en/latest/topics/channel_layers.html 32. https://channels.readthedocs.io/en/latest/topics/worker.html#sending 33. https://channels.readthedocs.io/en/latest/topics/channel_layers.html 34. https://channels.readthedocs.io/en/latest/topics/worker.html#receiving-and-consumers 35. https://channels.readthedocs.io/en/latest/topics/routing.html 36. https://channels.readthedocs.io/en/latest/topics/testing.html 37. https://channels.readthedocs.io/en/latest/deploying.html 38. https://www.sphinx-doc.org/ 39. https://github.com/readthedocs/sphinx_rtd_theme 40. https://readthedocs.org/

Hidden links: 42. https://channels.readthedocs.io/en/latest/index.html

;