fastapi example is wrong for db connect and close
>So those code can not set db connect before request processing and set db close for request processing over.

Peewee is connection-per-thread. FastAPI does not have such a concept -- rather it has an async context that may be interrupted at any time to switch to another request *within the same thread*. Therefore it's probably better to connect at startup. It's because of subtleties like this, along with the cargo-cult around asyncio, that I advise you to tread carefully.
there has wrong example for db connect and close

http://docs.peewee-orm.com/en/latest/peewee/database.html#fastapi

```python
from fastapi import FastAPI
from peewee import *

db = SqliteDatabase('my_app.db')
app = FastAPI()

# This hook ensures that a connection is opened to handle any queries
# generated by the request.
@app.on_event("startup")
def startup():
    db.connect()


# This hook ensures that the connection is closed when we've finished
# processing the request.
@app.on_event("shutdown")
def shutdown():
    if not db.is_closed():
        db.close()
```
however `@app.on_event("startup")` is for application not for request, and `@app.on_event("shutdown")` also for application stop not for request. So those code can not set db connect  before request processing and set db close for request processing over. 


 please reference fastapi document confirm this problem : https://fastapi.tiangolo.com/advanced/events/





peewee document can be reference [this ]( https://fastapi.tiangolo.com/advanced/sql-databases-peewee/#main-fastapi-app
)and then update fastapi example :

```python
    from fastapi import FastAPI
    from peewee import *
   
    import time
    from typing import List
    from fastapi import Depends, FastAPI, HTTPException
    app = FastAPI()
    sleep_time = 10
    db = SqliteDatabase('my_app.db')
    app = FastAPI()
    def get_db():
        try:
            db.connect()
            yield
        finally:
            if not db.is_closed():
                db.close()
    @app.post("/users/", dependencies=[Depends(get_db)])
    def create_user(user):
        pass
```