2016-10-18 09:22:49 +01:00
|
|
|
|
# Deploying
|
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
|
Deploying Sanic is made simple by the inbuilt webserver. After defining an
|
|
|
|
|
instance of `sanic.Sanic`, we can call the `run` method with the following
|
|
|
|
|
keyword arguments:
|
|
|
|
|
|
|
|
|
|
- `host` *(default `"127.0.0.1"`)*: Address to host the server on.
|
|
|
|
|
- `port` *(default `8000`)*: Port to host the server on.
|
|
|
|
|
- `debug` *(default `False`)*: Enables debug output (slows server).
|
|
|
|
|
- `ssl` *(default `None`)*: `SSLContext` for SSL encryption of worker(s).
|
|
|
|
|
- `sock` *(default `None`)*: Socket for the server to accept connections from.
|
|
|
|
|
- `workers` *(default `1`)*: Number of worker processes to spawn.
|
|
|
|
|
- `loop` *(default `None`)*: An `asyncio`-compatible event loop. If none is
|
|
|
|
|
specified, Sanic creates its own event loop.
|
|
|
|
|
- `protocol` *(default `HttpProtocol`)*: Subclass
|
|
|
|
|
of
|
2017-01-20 03:26:37 +00:00
|
|
|
|
[asyncio.protocol](https://docs.python.org/3/library/asyncio-protocol.html#protocol-classes).
|
2016-10-18 09:22:49 +01:00
|
|
|
|
|
|
|
|
|
## Workers
|
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
|
By default, Sanic listens in the main process using only one CPU core. To crank
|
|
|
|
|
up the juice, just specify the number of workers in the `run` arguments.
|
2016-10-18 09:22:49 +01:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
app.run(host='0.0.0.0', port=1337, workers=4)
|
|
|
|
|
```
|
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
|
Sanic will automatically spin up multiple processes and route traffic between
|
|
|
|
|
them. We recommend as many workers as you have available cores.
|
2016-10-18 09:22:49 +01:00
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
|
## Running via command
|
2016-10-18 09:22:49 +01:00
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
|
If you like using command line arguments, you can launch a Sanic server by
|
|
|
|
|
executing the module. For example, if you initialized Sanic as `app` in a file
|
|
|
|
|
named `server.py`, you could run the server like so:
|
2016-10-18 09:22:49 +01:00
|
|
|
|
|
|
|
|
|
`python -m sanic server.app --host=0.0.0.0 --port=1337 --workers=4`
|
|
|
|
|
|
2017-01-20 03:18:52 +00:00
|
|
|
|
With this way of running sanic, it is not necessary to invoke `app.run` in your
|
|
|
|
|
Python file. If you do, make sure you wrap it so that it only executes when
|
|
|
|
|
directly run by the interpreter.
|
2016-10-18 09:22:49 +01:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
app.run(host='0.0.0.0', port=1337, workers=4)
|
2017-01-20 03:18:52 +00:00
|
|
|
|
```
|
2017-03-23 01:12:23 +00:00
|
|
|
|
|
|
|
|
|
## Running via Gunicorn
|
|
|
|
|
|
|
|
|
|
[Gunicorn](http://gunicorn.org/) ‘Green Unicorn’ is a WSGI HTTP Server for UNIX.
|
|
|
|
|
It’s a pre-fork worker model ported from Ruby’s Unicorn project.
|
|
|
|
|
|
|
|
|
|
In order to run Sanic application with Gunicorn, you need to use the special `sanic.worker.GunicornWorker`
|
|
|
|
|
for Gunicorn `worker-class` argument:
|
|
|
|
|
|
|
|
|
|
```
|
2017-05-19 15:22:57 +01:00
|
|
|
|
gunicorn myapp:app --bind 0.0.0.0:1337 --worker-class sanic.worker.GunicornWorker
|
2017-03-23 01:12:23 +00:00
|
|
|
|
```
|
2017-05-03 06:44:42 +01:00
|
|
|
|
|
2017-06-22 21:26:50 +01:00
|
|
|
|
If your application suffers from memory leaks, you can configure Gunicorn to gracefully restart a worker
|
|
|
|
|
after it has processed a given number of requests. This can be a convenient way to help limit the effects
|
|
|
|
|
of the memory leak.
|
|
|
|
|
|
|
|
|
|
See the [Gunicorn Docs](http://docs.gunicorn.org/en/latest/settings.html#max-requests) for more information.
|
|
|
|
|
|
2017-05-03 06:44:42 +01:00
|
|
|
|
## Asynchronous support
|
|
|
|
|
This is suitable if you *need* to share the sanic process with other applications, in particular the `loop`.
|
|
|
|
|
However be advised that this method does not support using multiple processes, and is not the preferred way
|
|
|
|
|
to run the app in general.
|
|
|
|
|
|
|
|
|
|
Here is an incomplete example (please see `run_async.py` in examples for something more practical):
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
server = app.create_server(host="0.0.0.0", port=8000)
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
|
task = asyncio.ensure_future(server)
|
|
|
|
|
loop.run_forever()
|
|
|
|
|
```
|