title: How to make Django 3, channels and uvicorn work together
body: <p>I have been trying to switch from daphne to uvicorn for production with a project using django 3 and channels. I have been encountering errors while loading the classical asgi file for channels. Either I can't use it due to synchronous call to django.setup or get_application. I tried to tweak this file with the sync_to_async call without success. Has anyone managed to make it work ?</p>
  <h1>Original asgi.py</h1>
  <h2>Code</h2>
  <pre class="lang-py prettyprint-override"><code>import os
  import django
  from channels.routing import get_default_application
  
  
  os.environ.setdefault(&quot;DJANGO_SETTINGS_MODULE&quot;, &quot;config.settings&quot;)
  
  django.setup()
  application = get_default_application()
  </code></pre>
  <h2>StackTrace</h2>
  <pre><code>Traceback (most recent call last):                                                                                                                                              
  File &quot;/usr/local/lib/python3.8/multiprocessing/process.py&quot;, line 315, in _bootstrap                                                                                           
    self.run()                                                                                                                                                                  
  File &quot;/usr/local/lib/python3.8/multiprocessing/process.py&quot;, line 108, in run                                                                                                  
    self._target(*self._args, **self._kwargs)                                                                                                                                   
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/uvicorn/subprocess.py&quot;, line 61, in subprocess_started                           
    target(sockets=sockets)                                                                                                                                                     
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/uvicorn/main.py&quot;, line 407, in run                                               
    loop.run_until_complete(self.serve(sockets=sockets))                                                                                                                        
  File &quot;/usr/local/lib/python3.8/asyncio/base_events.py&quot;, line 612, in run_until_complete                                                                                       
    return future.result()                                                                                                                                                      
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/uvicorn/main.py&quot;, line 414, in serve                                             
    config.load()                                                                                                                                                               
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/uvicorn/config.py&quot;, line 300, in load                                            
    self.loaded_app = import_from_string(self.app)                                                                                                                              
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/uvicorn/importer.py&quot;, line 20, in import_from_string                             
    module = importlib.import_module(module_str)                                                                                                                                
  File &quot;/usr/local/lib/python3.8/importlib/__init__.py&quot;, line 127, in import_module                                                                                             
    return _bootstrap._gcd_import(name[level:], package, level)                                                                                                                 
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1014, in _gcd_import                                                                                                               
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 991, in _find_and_load                                                                                                             
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 975, in _find_and_load_unlocked                                                                                                    
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 671, in _load_unlocked                                                                                                             
  File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 783, in exec_module                                                                                                       
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 219, in _call_with_frames_removed                                                                                                  
  File &quot;./config/asgi.py&quot;, line 16, in &lt;module&gt;                                                                                                                                 
    django.setup()                                                                                                                                                              
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/django/__init__.py&quot;, line 24, in setup                                           
    apps.populate(settings.INSTALLED_APPS)                                                                                                                                      
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/django/apps/registry.py&quot;, line 122, in populate                                  
    app_config.ready()                                                                                                                                                          
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/django_prometheus/apps.py&quot;, line 23, in ready                                    
    ExportMigrations()                                                                                                                                                          
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/django_prometheus/migrations.py&quot;, line 52, in ExportMigrations                   
    executor = MigrationExecutor(connections[alias])                                                                                                                            
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/django/db/migrations/executor.py&quot;, line 18, in __init__                          
    self.loader = MigrationLoader(self.connection)                                                                                                                              
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/django/db/migrations/loader.py&quot;, line 49, in __init__                            
    self.build_graph()                                                                                                                                                          
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/django/db/migrations/loader.py&quot;, line 212, in build_graph                        
    self.applied_migrations = recorder.applied_migrations()                                                                                                                     
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/django/db/migrations/recorder.py&quot;, line 76, in applied_migrations                
    if self.has_table():                                                                                                                                                        
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/django/db/migrations/recorder.py&quot;, line 56, in has_table                         
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())                                                                 
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/django/utils/asyncio.py&quot;, line 24, in inner
    raise SynchronousOnlyOperation(message)
  django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
  </code></pre>
  <h1>Modified asgi.py</h1>
  <h2>Code</h2>
  <pre class="lang-py prettyprint-override"><code>import os
  import django
  from asgiref.sync import sync_to_async
  from channels.routing import get_default_application
  
  
  os.environ.setdefault(&quot;DJANGO_SETTINGS_MODULE&quot;, &quot;config.settings&quot;)
  sync_to_async(django.setup, thread_sensitive=True)
  application = sync_to_async(get_default_application, thread_sensitive=True)
  </code></pre>
  <h2>Stacktrace</h2>
  <pre><code>Traceback (most recent call last):
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/uvicorn/lifespan/on.py&quot;, line 55, in main
    await app(scope, self.receive, self.send)
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py&quot;, line 45, in __call__
    return await self.app(scope, receive, send)
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/asgiref/sync.py&quot;, line 296, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File &quot;/usr/local/lib/python3.8/asyncio/tasks.py&quot;, line 455, in wait_for
    return await fut
  File &quot;/usr/local/lib/python3.8/concurrent/futures/thread.py&quot;, line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File &quot;/var/www/.cache/pypoetry/virtualenvs/project-4ffvdAoS-py3.8/lib/python3.8/site-packages/asgiref/sync.py&quot;, line 334, in thread_handler
    return func(*args, **kwargs)
  TypeError: get_default_application() takes 0 positional arguments but 3 were given
  ERROR:    Application startup failed. Exiting.
  
  </code></pre>
  <p>Thanks for reading me guys</p>