Closed garyo closed 5 years ago
Hi @garyo
I think I see what you are asking, and yes it should be possible to do what you want.
Quick question.
Do you want to use the same contextualize context on the Blueprint as you are using on the Sanic App? Or a new/different context?
Do you want to create a new dbsessionmaker for the blueprint, or use the same one you created in setup_db()
when the app started?
I'd like to use the same contextualize context everywhere -- unless there's a good reason not to. The dbsessionmaker is global to the whole app, so it should be the same everywhere. I may create a dbsession (from the dbsessionmaker) on each request via middlewhere, but I haven't gotten that far yet. Still, that would be the same process for the blueprint as anywhere else.
Ok. I see your issue. Unfortunately what seems like a pretty simple thing to do is actually not so simple.
There are three main problems here:
register_blueprint()
.It comes down to the fact that blueprints are reusable templates meant to be used on any sanic app and on multiple sanic apps. You can install SPF on the blueprint, and register the contextualize plugin on it, and use it the same as you would on a sanic app, but the shared context that you get will be a different shared context to the one your sanic app uses.
Do you have an example of how you would do this in Flask? I would think that Flask has some of these same limitations too?
I'm trying to use the contextualize plugin to add a db engine to my app as in the example, and I have the plugin loading and registered using the
before_server_start
listener.I'm not clear on how to use contextualize with a websocket blueprint like this (in another file):