【发布时间】:2017-03-29 16:59:07
【问题描述】:
我的aiohttp 网络服务器使用随时间变化的全局变量:
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)
结果:
UnboundLocalError:分配前引用了局部变量“shared_item”
如何正确使用共享变量shared_item?
【问题讨论】:
-
您可以将类实例化为您的应用程序的命名空间。
标签: python python-3.x async-await python-asyncio aiohttp