-
-
Save saswata-dutta/53136342d7c85b466d0214008fa7ddeb to your computer and use it in GitHub Desktop.
Sharing memory across uwsgi workers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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