Skip to content

Instantly share code, notes, and snippets.

@redbo
Created February 16, 2012 02:21
Show Gist options
  • Save redbo/1841051 to your computer and use it in GitHub Desktop.
Save redbo/1841051 to your computer and use it in GitHub Desktop.
import mmap
import ctypes
class Record(ctypes.Structure):
_fields_ = [('index', ctypes.c_uint64),
('key', ctypes.c_char * 256)]
RECORD_COUNT = 256
storage_mem = mmap.mmap(-1, RECORD_COUNT * ctypes.sizeof(Record))
array = (Record * RECORD_COUNT).from_buffer(storage_mem)
for i in xrange(RECORD_COUNT):
array[i].index = i
array[i].key = 'item ' + str(i)
for i in xrange(RECORD_COUNT):
assert array[i].index == i
assert array[i].key == 'item ' + str(i)
@reversefold
Copy link

Check out https://gist.github.com/53405d856137175aa59d

Using c_wchar_p in the Array causes the child processes to use about a quarter the amount of memory that the parent process uses (if they read all of the data or a large chunk).

Using Record in the Array causes the child processes to use FAR less memory.....WTF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment