Share data between workers: Shared Area

You can share data between workers (for session, counters ecc. ecc.) using the -A option:

-A <n>

where <n> is the number of pages to allocate.

Supposing you need a 8k shared area on a system with 4k pages you will use

-A 2 

An additional shared area will be allocated for internal use (most locking/mutex functions).

You can now read/write this shared data importing the "uwsgi" module (embedded in the uWSGI server) and using some low-level functions:

import uwsgi

uwsgi.sharedarea_read(100,10)
uwsgi.sharedarea_readbyte(130)
uwsgi.sharedarea_writebyte(130, 17)
uwsgi.sharedarea_readlong(130)
uwsgi.sharedarea_writelong(117, 999999)
uwsgi.sharedarea_inclong(117)


The first argument is always the position (in bytes) in the shared area.

This area is completely SMP-safe as all the operations are governed by a pthread shared mutex (on OSX a spinlock is used).

The area is allocated with mmap:

sharedarea = mmap(NULL, getpagesize() * sharedareasize, PROT_READ|PROT_WRITE , MAP_SHARED|MAP_ANONYMOUS , -1, 0);

WARNING: The shared area is not supported on python 3.x