Skip to content

Instantly share code, notes, and snippets.

@bhtucker
Created March 31, 2016 18:58
Show Gist options
  • Save bhtucker/41dbed0a4be941989a878e9a754ea18e to your computer and use it in GitHub Desktop.
Save bhtucker/41dbed0a4be941989a878e9a754ea18e to your computer and use it in GitHub Desktop.
Sharing memory across uwsgi workers
"""
Simple worker showing different worker ids sharing/incrementing the same memory region
uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 --sharedarea 2
Then just keep refreshing localhost:9090
"""
import uwsgi
INT_ORDS = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57}
def memorystuff():
v = uwsgi.sharedarea_memoryview(0)
data = v.tobytes()
int_string = ''
for char in data:
char_ord = ord(char)
if char_ord not in INT_ORDS:
break
else:
int_string += char
int_data = int((int_string or '0'))
uwsgi.sharedarea_write(0, 0, str(int_data + 1))
return str(int_data)
def application(env, start_response):
memdat = memorystuff()
start_response('200 OK', [('Content-Type', 'text/html')])
return [str(uwsgi.worker_id()) + b"Hello World" + memdat]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment