Skip to content

Commit 9831678

Browse files
committed
Merge branch 'uvicorn'
2 parents 6a49df9 + 4360f38 commit 9831678

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ Same as the standard gunicorn configuration, but the application will be set for
101101

102102
_Note: Unlike the other servers, you have to configure gunicorn with environment variables or via `sys.argv`. If you use it with Django's `call_command`, keep in mind any additional arguments you pass will not be applied._
103103

104+
### With uvicorn
105+
106+
[uvicorn docs](https://www.uvicorn.org/)
107+
108+
Same as the standard uvicorn configuration, but the application will be set for you from `settings.WSGI_APPLICATION` as well as `--wsgi`.
109+
104110
### With waitress
105111

106112
[waitress docs](https://docs.pylonsproject.org/projects/waitress/en/latest/index.html)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from __future__ import absolute_import
2+
3+
import uvicorn.main
4+
5+
from ...base_command import WebserverCommand
6+
from ...utils import wsgi_app_name
7+
8+
9+
class Command(WebserverCommand):
10+
"""
11+
This bypasses any Django handling of the command and sends all arguments straight
12+
to uvicorn.
13+
"""
14+
15+
help = "Start uvicorn server"
16+
17+
def prep_server_args(self, argv):
18+
return argv[2:] + [wsgi_app_name(), "--wsgi"]
19+
20+
def start_server(self, *args):
21+
uvicorn.main.main(args)

django_webserver/tests/tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
def run_server(name, *args):
23+
pytest.importorskip(name)
2324
# timeout isn't supported in Python 2.7, do it the hard way...
2425
proc = subprocess.Popen(
2526
["django-admin", name] + list(args),
@@ -48,6 +49,15 @@ def test_gunicorn():
4849
assert "Booting worker with pid:" in output
4950

5051

52+
def test_uvicorn():
53+
proc = run_server("uvicorn", "--port=0")
54+
output = proc.communicate()[0].decode("utf-8")
55+
assert "Uvicorn running on http://127.0.0.1:" in output
56+
# :8000 is the default, ensure we aren't just seeing that
57+
assert "127.0.0.1:8000" not in output
58+
assert "Started server process [" in output
59+
60+
5161
def test_waitress():
5262
proc = run_server("waitress", "--port=0", "--host=127.0.0.1")
5363
output = proc.communicate()[0].decode("utf-8")

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ test = [
3030
]
3131
pyuwsgi = ["pyuwsgi"]
3232
gunicorn = ["gunicorn"]
33+
uvicorn = ["uvicorn; python_version >= '3.5'"]
3334
waitress = ["waitress"]

0 commit comments

Comments
 (0)