Skip to content

Commit

Permalink
one more update
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffckerr committed Mar 30, 2020
1 parent fdb87dc commit 46f423d
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,66 @@ python setup.py develop
11. To test if the if everything is working accordingly, open Python window within the virtual environment and type `import sciris` and `import scirisweb`. If no errors occur, then the import worked.


## Multhreaded deployment

The problem with the simple deployment method described above is that requests are single-threaded. If this is an issue, recommended deployment is using `nginx` to serve the static files, and `gunicorn` to run the Flask app. Note that it is common for an application to call several RPCs with each page load. This means that the multithreaded deployment can result in improved site performance even for a single user.

### Requirements

You must have nginx (`sudo apt install nginx`) and gunicorn (`pip install gunicorn`) installed.

### Set up nginx

1. Copy `examples/gunicorn/example_nginx_config` to e.g. `/etc/nginx/sites-enabled/my_app` (can change filename if desired)
2. Edit the copied file to specify
- The hostname/URL for the site e.g. `my_app.com`
- The full path to the directory containing `index.html` on the system running `nginx`
- Change the port in `proxy_pass` line if desired - it must match the port in `launch_gunicorn`
3. Reload or restart `nginx` e.g. `sudo service nginx reload`

For example, this will start it running at `localhost:8188`:

```script
server {
listen 8188;
server_name localhost;
location / {
root /home/my_username/my_sciris_app;
}
location /api {
proxy_pass http://127.0.0.1:8097/;
}
}
```

### Run gunicorn

1. Copy `examples/gunicorn/example_launch_gunicorn` to the folder with your app (e.g. `launch_my_app_gunicorn`), and set the number of workers as desired - usual recommendation is twice the number of CPUs but for applications that are CPU bound (e.g., an RPC call runs a model) then it may be better to reduce it to just the number of CPUs.
2. The example script references the Flask app using `name_of_your_app:flask_app`. The `name_of_your_app` should be importable in Python (either via running Python in the current directory, or installing as a package via `pip`) and `flask_app` is the name of a variable containing the Flask application. So for example, you might have a file `foo.py` containing

```python
app = sw.ScirisApp(__name__, name="My App")
the_app = app.flask_app
```
in which case the `launch_my_app_gunicorn` script should contain `foo:the_app` instead of `name_of_your_app:flask_app`.

3. Run `launch_my_app_gunicorn`. This will need to be kept running to support the site (so run via `nohup` or `screen` etc.).

For example:
```script
cd my_app
screen -S my_app_session
./launch_my_app_gunicorn
<you can now close the terminal>
...
<coming back later, you can restart it with>
screen -R my_app_session
```

Note that for local development, you can add the `--reload` flag to the `gunicorn` command to automatically reload the site. This can be helpful if using the `nginx+gunicorn` setup for local development.


## Examples

Expand Down

0 comments on commit 46f423d

Please sign in to comment.