Issue: FastAPI Framework Integration section (seems it) has incorrect information
Author: gondolio
State: closed
Created: 2021-05-04 04:06:13

In the [documentation](http://docs.peewee-orm.com/en/latest/peewee/database.html#framework-integration), it says

> We’ll open the connection when a request is received, then close it when the response is returned.

and it does this using "startup" and "shutdown" events. 

But if you look at the FastAPI [documentation](https://fastapi.tiangolo.com/advanced/events/), these events are triggered only when the **application** does startup/shutdown:

> To add a function that should be run before the application starts, declare it with the event "startup"
> To add a function that should be run when the application is shutting down, declare it with the event "shutdown"


You can confirm this by launching the server and seeing it only runs the startup() function once when it launches.



I'm not sure what the correct replacement is, but here is the Peewee page in FastAPI:
https://fastapi.tiangolo.com/advanced/sql-databases-peewee/

I'm not familiar with FastAPI, that section was contributed. Since its async I assume that it is, in fact, rather complicated to use Peewee correctly with it. You would want to have a connection-per-async-context presumably, or a pool of conns that each async context can utilize. For normal python this is easy -- we store the db conn in a threadlocal. This also works for gevent, which monkeypatches threadlocal to be green-thread local. For async stuff like FastAPI you're on your own in terms of figuring that out - you can scope your connection to synchronous blocks and that might work -- I suppose that's what the startup/shutdown event implies.

@coleifer async APIs become more and more popular. specifically FastAPI use is soaring. 
Any plans of supporting async apis which does not follow the thread per web request pattern? 

Thanks

There's no accounting for the multiplicity of developers tastes and opinions. My own view is that the pythonic way of doing async in python is to use gevent. Why?

* No need to change your codebase or litter it with `async ...`, `await`.
* No confusion around coroutines, tasks, futures, loops, etc.
* Works seamlessly with existing frameworks such as Django, Flask, etc.
* Because it handles switching implicitly, you don't need to worry if you forgot to yield somewhere.
* No need to modify or search for special "async" versions of popular libraries or clients.
* Works with database drivers as well, like pymysql and psycopg2.
* Uses the familiar threading APIs and provides a "greenlet"-local, locks, events, queues, etc.
* Performance
* https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/

Peewee supports the standard threading model, and by virtue of this, also supports gevent. At present I have no plans to make an async-aware version.

Thanks for the quick response @coleifer.

Well thats very unfortunate. ORMs should not force anything about the API tier. no matter how many benefits gevent has, 
I have my own constraints and reasons not to use it. either way as I said the python community is moving really fast toward async await, and it's gonna leave peewee behind.

As a peewee user who wants to use FastAPI / async await for my API I am currently in a problem.

I would really appreciate anything looking into this and supporting / making async-aware version for peewee.



>python community is moving really fast

Yes, it is moving fast (and breaking things). It seems that mindset has infected a lot of web-adjacent languages and communities.

It's always been my opinion that one should use the right tool for the job. Golang, for example, has a much better concurrency story by default for web development, as well as offering strong typing (something that Python also is flirting with). There are lots of languages out there. Rather than be all things to all people, Peewee plays to Python's strengths as I see them. Namely:

* Dynamism
* Simplicity, succintness
* Composability

There are various 3rd party libraries that provide async stuff for Peewee, but I cannot vouch for any personally. Google will help you find them.