Shared state with aiohttp web server

2024/9/30 11:36:53

My aiohttp webserver uses 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' referenced before assignment

How would I properly use a shared variable shared_item?

Answer

Push your shared variable into application's 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'
https://en.xdnf.cn/q/71089.html

Related Q&A

ModuleNotFoundError: No module named matplotlib.pyplot

When making a plot, I used both Jupyter Notebook and Pycharm with the same set of code and packages. The code is: import pandas as pd import numpy as np import matplotlib.pyplot as plt # as in Pycha…

python linux - display image with filename as viewer window title

when I display an image with PIL Image it opens an imagemagick window but the title is some gibberish name like tmpWbfj48Bfjf. How do I make the image filename to be the title of the viewer window?

Request body serialization differences when lambda function invoked via API Gateway v Lambda Console

I have a simple API set up in AWS API Gateway. It is set to invoke a Python 2.7 lambda function via API Gateway Proxy integration.I hit a strange error in that the lambda worked (processed the body co…

Determining a homogeneous affine transformation matrix from six points in 3D using Python

I am given the locations of three points:p1 = [1.0, 1.0, 1.0] p2 = [1.0, 2.0, 1.0] p3 = [1.0, 1.0, 2.0]and their transformed counterparts:p1_prime = [2.414213562373094, 5.732050807568877, 0.7320508075…

Split datetime64 column into a date and time column in pandas dataframe

If I have a dataframe with the first column being a datetime64 column. How do I split this column into 2 new columns, a date column and a time column. Here is my data and code so far:DateTime,Actual,Co…

Django - Setting date as date input value

Im trying to set a date as the value of a date input in a form. But, as your may have guessed, its not working.Heres what I have in my template:<div class="form-group"><label for=&qu…

ReduceLROnPlateau gives error with ADAM optimizer

Is it because adam optimizer changes the learning rate by itself. I get an error saying Attempting to use uninitialized value Adam_1/lr I guess there is no point in using ReduceLRonPlateau as Adam wil…

How to make add replies to comments in Django?

Im making my own blog with Django and I already made a Comments system.. I want to add the replies for each comment (like a normal comments box) and I dont know what to do this is my current models.py …

Which Regular Expression flavour is used in Python?

I want to know which RegEx-flavour is used for Python? Is it PCRE, Perl compatible or is it ICU or something else?

Python regex: Including whitespace inside character range

I have a regular expression that matches alphabets, numbers, _ and - (with a minimum and maximum length).^[a-zA-Z0-9_-]{3,100}$I want to include whitespace in that set of characters.According to the Py…