Skip to content

Instantly share code, notes, and snippets.

@UniIsland
Created May 7, 2012 04:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save UniIsland/2625945 to your computer and use it in GitHub Desktop.
Save UniIsland/2625945 to your computer and use it in GitHub Desktop.
python cross process data sharing with mmap
# Intro
extremely simple and unsophisticated cross process data sharing
supports one read-write master process and an arbitrary number of read-only processes
please consider using pickle/cPickle/ctype to store complex data
# References
http://blog.schmichael.com/2011/05/15/sharing-python-data-between-processes-using-mmap/
http://code.activestate.com/recipes/413807-communicate-between-processes-using-mmap/
#!/usr/bin/env python
import mmap
import os
import time
filename = '/path/to/mapfile'
fd = os.open(filename, os.O_RDONLY)
buf = mmap.mmap(fd, 0, mmap.MAP_SHARED, mmap.PROT_READ)
i = 0
while 1:
buf.seek(0)
i = int(buf.readline())
print i
time.sleep(3)
buf.close()
os.close(fd)
#!/usr/bin/env python
import mmap
import os
filename = '/path/to/mapfile'
## create and initialize file with code like this
#fd = os.open(filename, os.O_CREAT | os.O_TRUNC | os.O_RDWR)
#os.write(fd, '\x00' * mmap.PAGESIZE)
fd = os.open(filename, os.O_RDWR)
buf = mmap.mmap(fd, 0, mmap.MAP_SHARED, mmap.PROT_WRITE)
i = 0
while 1:
print i
buf.seek(0)
## use pickle to store complicated data
buf.write(str(i)+"\n")
#buf.flush()
i += 1
raw_input('ENTER')
buf.close()
os.close(fd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment