AttributeError: 'types.SimpleNamespace' object has no attribute 'session'

The program needs to connect to the database,It is error.

it is ok

async def index():
    red = Red()
    r = await red.get_redis_pool()
    user_num = 1
    await r.hmset('index', {'user_num': user_num})
app.register_listener(index, "after_server_start")

it is error

async def index(request):
    red = Red()
    r = await red.get_redis_pool()
    user_num = await User.find_users_num(request)
    await r.hmset('index', {'user_num': user_num})
app.register_listener(index, "after_server_start")

AttributeError: ‘types.SimpleNamespace’ object has no attribute ‘session’

The following code solves the problem, but each request will be executed once. I just want to execute it at startup

    @app.on_request
    async def index(request):
        red = Red()
        r = await red.get_redis_pool()
        print("==============")
        user_num = await User.find_users_num(request)
        await r.hmset('index', {'user_num': user_num})