General status with aiohttp web server

My web server aiohttpuses a global variable that changes over time:

from aiohttp import web    

shared_item = 'bla'

async def handle(request):
    if items['test'] == 'val':
        shared_item = 'doeda'
    print(shared_item)

app = web.Application()
app.router.add_get('/', handle)
web.run_app(app, host='somewhere.com', port=8181)

Results in:

UnboundLocalError: local variable 'shared_item' specified before assignment

How to use a shared variable shared_item?

+4
source share
1 answer

Click your shared variable in the application context:

async def handle(request):
    if items['test'] == 'val':
        request.app['shared_item'] = 'doeda'
    print(request.app['shared_item'])

app = web.Application()
app['shared_item'] = 'bla'
+4
source

Source: https://habr.com/ru/post/1660828/


All Articles