django / channels

Developer-friendly asynchrony for Django

Home Page:https://channels.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Channels and web concurrency

dan-developer opened this issue · comments

Good Morning.

I'm doing the following calculations in a view to use multiple processors:

cpus = multiprocessing.cpu_count()
processes = cpus - 2 if cpus > 2 else 1

with multiprocessing.Pool(processes=processes) as pool: 

    ...

    results = pool.starmap(_calculate_sf, data_to_simulate)

    pool.close()
    pool.join()

return redirect...

But there is a problem when I activate the "channels" app, which is: while there is a calculation running, new connections (another browser and ips (another clients)) are not accepted http processing (request -> processing -> response). It is waiting, while it does not finish the calculation, it does not respond to more connections. I had to return to the WSGI application which the problem does not occur.

  • Ubuntu 18.04, Ubuntu 20.04 and MacOS 12.2 with Docker Desktop.
  • Relevant requirements:
daphne==3.0.1
Django==3.2
django-celery-results==2.0.0
channels==3.0.2
channels-redis==3.2.0
  • Calculate without freeze other connections.
  • How you're running Channels: runserver
  • No errors.

Yes… Under Django 3.x, requests to Django views are essentially single-threaded, due to the thread sensitive nature of ORM calls wrapped in sync_to_async. Using WSGI here is the right thing to do.

You can try Django 4.0, which introduces a per-request async context. It would be interesting to see how that compares for you.