Created
May 7, 2012 04:38
-
-
Save UniIsland/2625945 to your computer and use it in GitHub Desktop.
python cross process data sharing with mmap
This file contains 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
# 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/ |
This file contains 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
#!/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) |
This file contains 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
#!/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