Skip to main content

Async Django Session

It gives you ability to share session between django and an async framework like aiohttp, starlette, fastapi etc.

pip install async-django-session

API

Backends

There's two ways of communicating to database available:

  • through databases - which is compatible with most of major RDBMS:
    database = databases.Database(DB_URI)
    await database.connect()
    backend = async_django_session.databases.Backend(database, SECRET_KEY)
    
  • or directly through asyncpg (PostgreSQL only):
    pool = await asyncpg.create_pool(DB_URI)
    backend = async_django_session.asyncpg.Backend(pool, SECRET_KEY)
    

Session

To fetch session from db by its key there's backend.get_session method. If key is None a new session will be created:

session = backend.get_session(key)

It's lazy so the session data won't be actually fetched until you call its load method. In caches the result, so it's inexpensive to call it multiple times:

await session.load()

You can combine them into a single line as the load method returns session itself:

session = await backend.get_session(key).load()

Session provides dict-interface to read / write data:

session["foo"] = "bar"
print(session["foo"])

To sync session with database you should explicitly call its save method. It won't make unnecessary db call if the session wasn't changed (the boolean value it returns is intend to indicate if it was the case).

saved = await session.save()

During saving of a new session a random key will be generated and available as session.key parameter afterwords.

Frameworks integration

There's built-in middlewares for a few frameworks to automatically load (using session id from cookies) and save sessions.

Aiohttp

After adding of session middleware:

session_middleware = async_django_session.aiohttp.middleware(
    async_django_session.databases.Backend(db, SECRET_KEY)
)
app = web.Application(middlewares=[session_middleware])

You can get requests session as:

session = await request.get_session()

A full aiohttp example can be found here.

Starlette

After adding of session middleware:

async_django_session.starlette.middleware(
    app, async_django_session.databases.Backend(db, SECRET))
)

Session of a current request is available as:

session = await request.state.get_session()

A working starlette example is here.

Fastapi

Perform starlette app initialization from above as fastapi based on it. After that you can get session using dependency injection as:

from async_django_session.fastapi import get_session
from async_django_session.session import Session

async def index(session: Session = Depends(get_session)):
    ...

A working fastapi example is here.

Running examples

Running the examples you can see different frameworks using the same session data. To see session data open http://localhost:8000/ after running of each example.

Install requirements:

cd examples
pip install -r requirements.txt

Create database and tables:

createdb async_django_session
python django_app.py migrate

Run aiohttp example which uses databases backend:

python aiohttp_app.py

Run starlette example which uses asyncpg backend:

python starlette_app.py

Run fastapi example which uses asyncpg backend:

python fastapi_app.py

Run django example:

python django_app.py runserver

Release files for async-django-session 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for async-django-session 0.3.1
File Interpreter ABI Platform
async_django_session-0.3.1-py3-none-any.whl Python 3 none any Details

Release files / async_django_session-0.3.1-py3-none-any.whl

Download URL async_django_session-0.3.1-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26c963c03a5f66eb1772c546043edf4baa3075bb5f358efec4e570d62cd071a0
BLAKE2b-256 checksum
How to use checksums
be2752d62c1f9aa44071f8ee612bbf7e52ec4938e8ee09d54852bf9f8fbfd082
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.3.1 This release

1 release file

0.3.0

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page