Channels -- a question about how ProtocolTypeRouter works

51 views
Skip to first unread message

Zhiyu/Drew Li

unread,
Oct 26, 2018, 5:55:46 PM10/26/18
to Django users
Hi,

Newbie here. (please bear with me if this a stupid question)
I am going through the tutorial and doc and got this question regarding how ProtocolTypeRouter works.
ProtocolTypeRouter.__init__ takes a dict "application_mapping" and append a key-value pair to it: "http': class AsgiHandler
As doc says ASGI application is created on a per scope basis, I think I vaguely understand how the "http" one works. 
Once a http scope comes in, an instance/object of class AsgiHandler is initiated, which lasts for the duration of the scope.
So it is one scope leads to one ASGI application/object

But I am confused about this one:
application = ProtocolTypeRouter({
    # (http->django views is added by default)
    'websocket': AuthMiddlewareStack(
        URLRouter(
            chat.routing.websocket_urlpatterns
        )
    ),
})
The whole value of this key-value pair AuthMiddlewareStack(URLRouter(chat.routing.websocket_urlpatterns)) seems to be a object not a class, 
so that means the application already exists before a websocket scope comes in. How does this conform to "one scope one application" rule?

Any help would be appreciated.

Thanks
Drew

Andrew Godwin

unread,
Oct 26, 2018, 6:07:58 PM10/26/18
to django...@googlegroups.com
Hi Drew,

The return value is indeed an object, but if you look closely at it you'll see it defines a __call__ method - meaning that when it's called as an ASGI application, it then returns a further object to act inside the scope.

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f6088bd4-457a-4106-b883-332af9a99337%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Zhiyu (Drew) Li

unread,
Oct 26, 2018, 6:18:06 PM10/26/18
to django...@googlegroups.com
Thanks!  So that means some objects last across multiple scopes, and they can create another obj that only lasts for one specific scope?
I am still confused how the different layers of middleware wrap around the real asgi application -- a obj just lasts for one scope. 
I need to go through the code deeper.  

Thanks
Drew



Andrew Godwin

unread,
Oct 26, 2018, 6:33:33 PM10/26/18
to django...@googlegroups.com
I would instead think of the object that ends up there as a factory that makes the final objects that end up per-scope. It's slightly confusing, I will grant you, but in Python anything can make a new class instance, not just a class constructor, which is what's happening here.

Andrew

Reply all
Reply to author
Forward
0 new messages