| 1 |
Reload on code changes |
| 2 |
====================== |
| 3 |
|
| 4 |
When developing a websockets server, you are likely to run it locally to test |
| 5 |
changes. Unfortunately, whenever you want to try a new version of the code, you |
| 6 |
must stop the server and restart it, which slows down your development process. |
| 7 |
|
| 8 |
Web frameworks such as Django or Flask provide a development server that reloads |
| 9 |
the application automatically when you make code changes. There is no equivalent |
| 10 |
functionality in websockets because it's designed only for production. |
| 11 |
|
| 12 |
However, you can achieve the same result easily with a third-party library and a |
| 13 |
shell command. |
| 14 |
|
| 15 |
Install watchdog_ with the ``watchmedo`` shell utility: |
| 16 |
|
| 17 |
.. code-block:: console |
| 18 |
|
| 19 |
$ pip install 'watchdog[watchmedo]' |
| 20 |
|
| 21 |
.. _watchdog: https://pypi.org/project/watchdog/ |
| 22 |
|
| 23 |
Run your server with ``watchmedo auto-restart``: |
| 24 |
|
| 25 |
.. code-block:: console |
| 26 |
|
| 27 |
$ watchmedo auto-restart --pattern "*.py" --recursive --signal SIGTERM \ |
| 28 |
python app.py |
| 29 |
|
| 30 |
This example assumes that the server is defined in a script called ``app.py`` |
| 31 |
and exits cleanly when receiving the ``SIGTERM`` signal. Adapt as necessary. |