Adding ASGI support to runserver

Sorry to bring back an old thread but I was just wondering is there any way to run an async runserver in django 3.2? I can’t find anything in the docs.
I am using at the moment but I need to collectstatic my files locally for django admin files to be served. Is there a better solution?

There is no way in the core. The easiest way is probably installing channels and installing it only for local dev, while using whatever other server you want in production.

I didn’t realize that Carlton accepted the ticket about a year ago for getting something into the core. I’ll try and make a pr at least updating the docs to suggest installing channels for local development, and then a pr with some code for anything new.

Yep, echoing this. Channels has a runserver here which you can use.

(Cough) We should be able to build something to handle at least http protocol requests from Python’s http.server. I began looking at this but haven’t had capacity to see it through. That Channels has a runserver means it’s not been pressing. (And 2020/21 :flushed:) — Also @massover’s project looked very good IIRC.

In general, no problem raising the old threads: things can and do move slowly in Django-land — glacially, inexorably, … :grinning: — it’s good to keep the discussions together where possible.

Yeah, the key thing here is that Python ships a basic WSGI server in the core set of libraries (wsgiref), but asgiref is a) separate and b) doesn’t contain a reference server, as it’s quite a bit more code to do so.

I think it’s a good idea and we should get there - potentially adding the basic HTTP server required to asgiref if we need, since Django already imports that - but it’s not around yet.

OK… finally approaching the (current) bottom of the Pandemic Backlog™:sweat_smile:

I’ve begun work on what will be Channels v4 (hoping to release, along with Daphne and channels_redis updates around Sept.) Part of this is removing all the AsgiHandler &co code from Channels, instead relying there on Django’s versions, which will be the way forward.

That will leave Channels nice and tight, as (just) the Websockets and HTTP Consumers, Channel Layer, and maybe a few other ASGI utils bits, but little more than that.

One part of this that I think folks would appreciate is breaking the hard dependency on Daphne (and so Twisted). Moving the runserver code out of Channels is required for that.

@massover Would you be up for inputting on the suggested django-asgi-runserver repo, if I create one under the Django org? (No stress if you don’t have capacity, but given the work you already put in, maybe you’d be keen? :slightly_smiling_face:) Ideally we’d be able to remove this package over time — as per the discussion here — but likely it would be around for a couple of cycles (or more) at least.

The Channels runserver is now updated to use django.contrib.staticfiles’s ASGIStaticFilesHandler so creating the package should (:grimacing:) be a question of migrating that out, and so on.

The previous blocker here was making it easy for beginners to get up and running. I think something like…

pip install channels['runserver']

Plus an adjustment to INSTALLED_APPS is bearable, given I can’t see another way out of that.

Then other things to consider are:

  • The INSTALLED_APPS order so we don’t fight django.contrib.staticfiles.
    • Clear instructions there win.
    • A system check would be a nice-to-have. (Need to check they’re run on every code path…)
    • I’d like to remove the hack code from Channels that monkey patches the django.contrib.staticfiles runserver if we can.
  • The early import for the reactor setup. This is probably fine in the — as currently — which is done before middleware are loaded (which is the other suspect).

(These last are mainly notes to self.)

Update: We could just transfer it and keep the name as is… :thinking:

This might run too. It may be simpler.

Update: Move runserver command into Daphne. · Issue #428 · django/daphne · GitHub

@carltongibson I have some time, I’ll have a look

Hey @massover — great.

I began here: Added runserver to Daphne. by carltongibson · Pull Request #429 · django/daphne · GitHub — just making Daphne into a Django app so far. If you want to collaborate on that branch, that would be awesome.