2021/03/17

Walkthrough the basic of Python Channels

Goal of writing this blog

  • Figure out terminologies in Channels project
    • For example, Consumers, Channel layer
  • As an ASGI application, how Channels bridge up with existing Django codes
  • Expressed in code path for traffic in and out between Django and Daphne
  • What are the interactions between Channels layer, and Consumers

Terminologies

Consumers[1][2]

  • basic consumers - individual pieces that might handle chat messaging, or notifications - and tie them together with URL routing, protocol detection and other handy things to make a full application.
  • A consumer is the basic unit of Channels code. We call it a consumer as it consumes events, but you can think of it as its own tiny little application.

  • Consumers can be either long running / short running depends on the scope that consumer handling

  • a rich abstraction that allows you to make ASGI applications easily.

(Channels) Router[1][2]

  • A way to combine multiple consumers into 1 asgi application
  • Channels router works on scope level instead of event level
    • Distribute per scope instead of per event

Channel Layer[1][2]

  • channel layer, a low-level abstraction around a set of transports that allow you to send information between different processes.
  • A solution to communicate between different application instances

  • Each application should has an unique channel name

  • Allows both point-to-point, and broadcast messaging

Worker

  • A solution from project Channels, which running as a standalone process, on processing some naive background tasks. A worker will listen, and fire events from databases (Redis, Postgres etc) through Channel Layer
  • IMO, we can view this as kind of a celery worker

Channels and Django

From Daphne to Django

  • Invoke through … django asgi application definitnion …
  • channels.routing.ProtocolTypeRouter
  • channels.http.AsgiHandler
  • … Corresponding django view …

From Django to Daphne

  • … Corresponding Django view …
  • ….
  • channels.http.AsgiHandler.get_response
  • … Call send() which come from Daphne …

Ecosystem of Channels

1 則留言: