Skip to content

Commit

Permalink
Fix formatting and minor wording issues in docs (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
illagrenan authored Oct 29, 2024
1 parent 462c6d2 commit 6504575
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def example_view(request) -> HttpResponse:

## Quickstart

1. Python `^3.10 || ^3.11 || ^3.12 || ^3.13` and Django `^4.2 || ^5.0.3 || ^5.1` are supported. To install this package run:
1. Python `^3.10 || ^3.11 || ^3.12 || ^3.13` and Django `^4.2 || ^5.0.3 || ^5.1` are supported. To install this package, run:
```console linenums="0"
poetry add django-asgi-lifespan@latest
```
Expand Down
2 changes: 1 addition & 1 deletion docs/asgi.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To provide asynchronous support for the project, an ASGI server is necessary. Th

## Uvicorn :material-star:

Uvicorn is the most tested ASGI server by the author of this plugin. The author uses uvicorn for development, testing and production. In production it is perfectly fine to use uvicorn without gunicorn, see:
Uvicorn is the most tested ASGI server by the author of this plugin. The author uses uvicorn for development, testing and production. In production it is perfectly fine to use uvicorn without gunicorn, see:

* <https://stackoverflow.com/questions/66362199/what-is-the-difference-between-uvicorn-and-gunicornuvicorn/71546833>
* </~https://github.com/encode/uvicorn/issues/303>
Expand Down
60 changes: 30 additions & 30 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@ Ready to contribute? Here's how to set up `django-asgi-lifespan` for local devel
1. Fork the `django-asgi-lifespan` repo on GitHub.
2. Clone your fork locally

```
$ git clone git@github.com:your_name_here/django-asgi-lifespan.git
``` console linenums="0"
git clone git@github.com:your_name_here/django-asgi-lifespan.git
```

3. Ensure [Poetry](https://python-poetry.org/docs/) is installed.
4. Install dependencies and start your virtualenv:
```
$ poetry install
``` console linenums="0"
poetry install
```

5. Create a branch for local development:

```
$ git checkout -b name-of-your-bugfix-or-feature
``` console linenums="0"
git checkout -b name-of-your-bugfix-or-feature
```

Now you can make your changes locally.

6. When you're done making changes, check that your changes pass the
tests, including testing other Python versions, with tox:
tests:

```
$ task test
``` console linenums="0"
task test
```

7. Commit your changes and push your branch to GitHub:

```
$ git add .
$ git commit -m "Your detailed description of your changes."
$ git push origin name-of-your-bugfix-or-feature
``` console
git add .
git commit -m "Your detailed description of your changes."
git push origin name-of-your-bugfix-or-feature
```

8. Submit a pull request through the GitHub website.
Expand All @@ -47,35 +47,35 @@ Ready to contribute? Here's how to set up `django-asgi-lifespan` for local devel

## Formatting and linting

```
$ poetry task format
``` console linenums="0"
poetry task format
```

## Tests

```
$ poetry task test
``` console linenums="0"
poetry task test
```

## Run a Django test project locally

The tests include a Django test project for integration testing. You can also run this test project locally (like any other Django project), sometimes it's useful to observe how a real application behaves.

```
$ cd ./tests/
$ poetry run uvicorn django_test_application.asgi:application --log-level=debug --reload
$ curl -v http://127.0.0.1:8000/client-from-app-config
$ curl -v http://127.0.0.1:8000/client-from-scope-state
``` console
cd ./tests/
poetry run uvicorn django_test_application.asgi:application --log-level=debug --reload
curl -v http://127.0.0.1:8000/client-from-app-config
curl -v http://127.0.0.1:8000/client-from-scope-state
```

## Test ASGI servers

```
$ cd ./tests/
$ poetry run uvicorn django_test_application.asgi:application --log-level=debug --reload
$ poetry run hypercorn django_test_application.asgi:application --log-level debug --reload
$ poetry run daphne django_test_application.asgi:application
$ poetry run granian --interface asgi django_test_application.asgi:application
$ poetry run gunicorn django_test_application.asgi:application -k uvicorn.workers.UvicornWorker --log-level debug --reload
``` console
cd ./tests/
poetry run uvicorn django_test_application.asgi:application --log-level=debug --reload
poetry run hypercorn django_test_application.asgi:application --log-level debug --reload
poetry run daphne django_test_application.asgi:application
poetry run granian --interface asgi django_test_application.asgi:application
poetry run gunicorn django_test_application.asgi:application -k uvicorn.workers.UvicornWorker --log-level debug --reload
```

4 changes: 2 additions & 2 deletions docs/how_to_use/context_manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<figcaption>Simplified sequence diagram. <a href="#sequence-diagram">See the full version of the diagram</a>.</figcaption>
</figure>

When you execute the Django project using the ASGI server (like uvicorn), it sends lifespan events at its startup. These lifespan events are ignored by the standard Django ASGI handler. The lifespan events include the lifespan scope state, which is a Python dictionary that is preserved by the ASGI server and allowed to be modified by the developer. Hence, it's a suitable location for storing global application states or shared objects. For instance, one could create a shared HTTPX async client to implement connection pooling.
When you execute the Django project using the ASGI server (like uvicorn), it sends lifespan events at its startup. These lifespan events are ignored by the standard Django ASGI handler. The lifespan events include the lifespan scope state, which is a Python dictionary that is preserved by the ASGI server and allowed to be modified by the developer. Hence, it's a suitable location for storing global application states or shared objects. For instance, one could create a shared HTTPX async client to implement connection pooling.


## Accessing the shared state
Expand All @@ -31,7 +31,7 @@ You must define an [async context manager](https://docs.python.org/3/reference/d

## Registering the context manager

The manager that you have just defined needs to be registered. The most suitable location for registration is [Django AppConfig](https://docs.djangoproject.com/en/dev/ref/applications/#application-configuration).
The manager that you have just defined needs to be registered. The most suitable location for registration is [Django AppConfig](https://docs.djangoproject.com/en/dev/ref/applications/#application-configuration).

``` py hl_lines="12-14" title="apps.py"
--8<-- "example/managers_app.py"
Expand Down
8 changes: 4 additions & 4 deletions docs/how_to_use/signals.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

## Before you start

This low-level approach uses ASGI lifespan signals directly. The disadvantage is that the developer has to figure out where to store the global shared state. Prefer the more modern approach via [context manager](context_manager.md) &mdash; the global state will be managed by the ASGI server.
This low-level approach uses ASGI lifespan signals directly. The disadvantage is that the developer has to figure out where to store the global shared state. Prefer the more modern approach via [context manager](context_manager.md) &mdash; the global state will be managed by the ASGI server.

## Example use case for lifespan signals

Consider a [Django application](https://docs.djangoproject.com/en/dev/ref/applications/) named `library`. This application retrieves book information from an external API using [HTTPX](https://www.python-httpx.org/). To ensure efficient utilization of network resources (e.g., connection pooling, see: <https://www.python-httpx.org/advanced/#why-use-a-client>), we intend to use the HTTPX client.
Consider a [Django application](https://docs.djangoproject.com/en/dev/ref/applications/) named `library`. This application retrieves book information from an external API using [HTTPX](https://www.python-httpx.org/). To ensure efficient utilization of network resources (e.g., connection pooling, see: <https://www.python-httpx.org/advanced/#why-use-a-client>), we intend to use the HTTPX client.

# Typehinting

Firstly, we're going to define a [Protocol](https://docs.python.org/3/library/typing.html#typing.Protocol) for typehinting.

This Protocol (`#!python HTTPXAppConfig`) provides an explicit interface for a request that contains a reference to an instance of `#!python httpx.AsyncClient` in its `#!python state` dictionary.
This Protocol (`#!python HTTPXAppConfig`) provides an explicit interface for a request that contains a reference to an instance of `#!python httpx.AsyncClient` in its `#!python state` dictionary.

``` py hl_lines="6-7" title="types.py"
--8<-- "example/types.py"
```

# Signal receivers

Next, we create receivers for lifespan signals. A startup receiver that creates an instance of the HTTPX client and a shutdown receiver that closes the client.
Next, we create receivers for lifespan signals. A startup receiver that creates an instance of the HTTPX client, and a shutdown receiver that closes the client.

``` py hl_lines="13 16" title="handlers.py"
--8<-- "example/handlers.py"
Expand Down
12 changes: 6 additions & 6 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

To install Django ASGI Lifespan, run following command in your terminal:

=== ":simple-poetry: Poetry"
=== ":simple-poetry: Poetry"

```console linenums="0"
``` console linenums="0"
poetry add django-asgi-lifespan@latest
```

=== ":simple-python: pip"

```console linenums="0"
``` console linenums="0"
pip install --upgrade django-asgi-lifespan
```

Expand All @@ -21,12 +21,12 @@ This is the preferred method to install this package, as it will always install
# From source

The source for Django ASGI Lifespan can be downloaded from
the [Github repo](/~https://github.com/illagrenan/django-asgi-lifespan).
the [Github repository](/~https://github.com/illagrenan/django-asgi-lifespan).

You can either clone the public repository:

``` console linenums="0"
$ git clone git://github.com/illagrenan/django-asgi-lifespan
git clone git://github.com/illagrenan/django-asgi-lifespan
```

Or download the [tarball](/~https://github.com/illagrenan/django-asgi-lifespan/tarball/master):
Expand All @@ -42,5 +42,5 @@ curl --proto '=https' --tlsv1.3 -fsSL \
Once you have a copy of the source, you can install it with:

``` console linenums="0"
$ pip install .
pip install .
```
2 changes: 1 addition & 1 deletion docs/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
--verbose --allow-dirty --dry-run
```
This uses [bump-my-version](/~https://github.com/callowayproject/bump-my-version).
4. If everything looks OK, run the same command as in the previous step, but without the `--dry-run`switch.
4. If everything looks OK, run the same command as in the previous step, but without the `--dry-run` switch.
5. Push the newly created tag:
``` console linenums="0"
git push --verbose origin --tags
Expand Down

0 comments on commit 6504575

Please sign in to comment.