Sanic Cookie and Session Management
Project description
Sanic Cookies
Code here is mostly borrowed from sanic_session.
I wanted to make some changes that would break a big part of sanic_session
's API, so I decided to create this repo instead.
Sanic cookies supports both client side and server side cookies.
Some of the main deviations from sanic_session are:
-
Interfaces are only responsible for reading/writing the
session_dict
. Session management logic is handled by the session object -
No race conditions:
By using:
async with request['session']: request['session']['foo'] = 'bar'
instead of:
request['session']['foo'] = 'bar'
It is still however possible to use the
session_dict
without a context manager, but it will raise some warnings, unless it's explicitly turned off (warn_lock=False) -
A more simple implementation of SessionDict that helps me sleep in peace at night. (Probably less performant)
-
In memory interface schedules cleanup to avoid running out of memory
-
Client side cookies
Quick Start
from sanic_cookies import Session, InMemory
interface = InMemory()
app = Sanic()
Session(app, master_interface=interface)
# You can skip this part if you don't want scheduled interface cleanup
@app.listener('before_server_start')
def init_inmemory(app, loop):
interface.init()
@app.listener('after_server_stop')
def kill_inmemory(app, loop):
interface.kill()
@app.route('/')
async def handler(request):
async with request['session'] as sess:
sess['foo'] = 'bar'
If enough people are interested, I can write some docs. Meanwhile, if you excuse me...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.