def run_writer3(reactor, shmfile, size=1024): print("run_writer", reactor.__class__, shmfile) if False: if os.path.exists(shmfile): print("removing existing file") os.remove(shmfile) with open(shmfile, 'wb+') as fd: fd.write(DATA) fd.truncate(size) fd.flush() return succeed(None) with open(shmfile, 'rw+') as fd: # m = cmmap.mmap(prot=cmmap.PROT_READ, length=len(data), flags=cmmap.MAP_SHARED, fd=f.fileno()) # shm = cmmap.mmap(prot=cmmap.PROT_WRITE, length=size, flags=cmmap.MAP_SHARED, fd=fd.fileno(), buffer=False) # shm = cmmap.mmap(prot=cmmap.PROT_WRITE, length=size, flags=cmmap.MAP_SHARED, fd=fd.fileno(), buffer=True) shm = cmmap.mmap(prot=cmmap.PROT_READ|cmmap.PROT_WRITE, flags=cmmap.MAP_SHARED, length=size, fd=fd.fileno(), buffer=True) print(shm, type(shm), len(shm)) #shm[0:len(DATA)] = DATA print(binascii.hexlify(shm[0:10])) #msg = ffi.cast("wamp_msg_subscribed_t*", shm) #print fd, shm, type(shm), msg, type(msg) #msg.request = 23 #msg.subscription = 666 #print("msg: {} {}".format(msg.request, msg.subscription)) return succeed(None)
def test_anonymous_private(): data = 'foobarding' m = cmmap.mmap(length=10, prot=cmmap.PROT_READ|cmmap.PROT_WRITE, flags=cmmap.MAP_ANONYMOUS|cmmap.MAP_PRIVATE) m[:] = data assert m[:] == data
def run_writer3(reactor, shmfile, size=1024): print("run_writer", reactor.__class__, shmfile) if False: if os.path.exists(shmfile): print("removing existing file") os.remove(shmfile) with open(shmfile, 'wb+') as fd: fd.write(DATA) fd.truncate(size) fd.flush() return succeed(None) with open(shmfile, 'rw+') as fd: # m = cmmap.mmap(prot=cmmap.PROT_READ, length=len(data), flags=cmmap.MAP_SHARED, fd=f.fileno()) # shm = cmmap.mmap(prot=cmmap.PROT_WRITE, length=size, flags=cmmap.MAP_SHARED, fd=fd.fileno(), buffer=False) # shm = cmmap.mmap(prot=cmmap.PROT_WRITE, length=size, flags=cmmap.MAP_SHARED, fd=fd.fileno(), buffer=True) shm = cmmap.mmap(prot=cmmap.PROT_READ | cmmap.PROT_WRITE, flags=cmmap.MAP_SHARED, length=size, fd=fd.fileno(), buffer=True) print(shm, type(shm), len(shm)) #shm[0:len(DATA)] = DATA print(binascii.hexlify(shm[0:10])) #msg = ffi.cast("wamp_msg_subscribed_t*", shm) #print fd, shm, type(shm), msg, type(msg) #msg.request = 23 #msg.subscription = 666 #print("msg: {} {}".format(msg.request, msg.subscription)) return succeed(None)
def test_file(): fd, filename = tempfile.mkstemp() f = open(filename, 'w') data = 'foobarbing' f.write(data) f.close() f = open(filename, 'r+') m = cmmap.mmap(prot=cmmap.PROT_READ, length=len(data), flags=cmmap.MAP_SHARED, fd=f.fileno()) assert m[:] == data
def __init__(self, filename): if not os.path.exists(filename): raise Exception("path {} does not exist".format(filename)) size = os.stat(filename).st_size with open(filename, 'rb') as fd: self._map = cmmap.mmap(prot=cmmap.PROT_READ, length=size, flags=cmmap.MAP_SHARED, fd=fd.fileno(), buffer=False)
def test_file(): fd, filename = tempfile.mkstemp() f = open(filename, 'wb') data = b'\x07\0\0\0\0\0\0\0foobarbing' f.write(data) f.close() f = open(filename, 'rb+') m = cmmap.mmap(prot=cmmap.PROT_READ, length=len(data), flags=cmmap.MAP_SHARED, fd=f.fileno(), buffer=False) m = ffi.cast("wamp_msg_subscribed_t*", m) print m print m.request
def test_anonymous_shared(): data = 'foobarding' m = cmmap.mmap(length=10, prot=cmmap.PROT_READ|cmmap.PROT_WRITE, flags=cmmap.MAP_ANONYMOUS|cmmap.MAP_SHARED) m[:] = data assert m[:] == data def f(): assert m[:] == data m[0] = 'd' proc = Process(target=f) proc.start() proc.join() assert m[:] == 'doobarding'
def run_writer2(reactor, shmfile, size=1000): print("run_writer", reactor.__class__, shmfile) if not os.path.exists(shmfile): with open(shmfile, 'wb') as out: out.truncate(size) with open(shmfile, 'wb') as fd: shm = cmmap.mmap(prot=cmmap.PROT_WRITE, length=size, flags=cmmap.MAP_SHARED, fd=fd.fileno(), buffer=False) msg = ffi.cast("wamp_msg_subscribed_t*", shm) #msg.request = 23 #msg.subscription = 666 print("msg: {} {}".format(msg.request, msg.subscription)) return succeed(None)
def run_reader(reactor, shmfile, size=1024): print("run_reader", reactor.__class__, shmfile) area = ShmArea(shmfile) msg = area.get(0) print msg.msg_type, msg.request, ffi.string(msg.uri) msg = area.get(OFFSET) print msg.msg_type, msg.request, msg.subscription return succeed(None) with open(shmfile, 'rb') as fd: shm = cmmap.mmap(prot=cmmap.PROT_READ, length=size, flags=cmmap.MAP_SHARED, fd=fd.fileno(), buffer=False) msg = ffi.cast("wamp_msg_subscribed_t*", shm) print("msg: {} {}".format(msg.request, msg.subscription)) return succeed(None)
def run_writer(reactor, shmfile, size=1024): print("run_writer", reactor.__class__, shmfile) with open(shmfile, 'rw+') as fd: shm = cmmap.mmap(prot=cmmap.PROT_READ|cmmap.PROT_WRITE, flags=cmmap.MAP_SHARED, length=size, fd=fd.fileno(), buffer=False) msg = ffi.cast("wamp_msg_subscribe_t*", shm) msg.msg_type = 2 msg.request = 23 msg.uri = u'com.example.add2' msg.foo = 255 msg = ffi.cast("wamp_msg_subscribed_t*", shm + OFFSET) msg.msg_type = 3 msg.request = 23 msg.subscription = 666 print("msg: {} {}".format(msg.request, msg.subscription)) return succeed(None)
def run_writer(reactor, shmfile, size=1024): print("run_writer", reactor.__class__, shmfile) with open(shmfile, 'rw+') as fd: shm = cmmap.mmap(prot=cmmap.PROT_READ | cmmap.PROT_WRITE, flags=cmmap.MAP_SHARED, length=size, fd=fd.fileno(), buffer=False) msg = ffi.cast("wamp_msg_subscribe_t*", shm) msg.msg_type = 2 msg.request = 23 msg.uri = u'com.example.add2' msg.foo = 255 msg = ffi.cast("wamp_msg_subscribed_t*", shm + OFFSET) msg.msg_type = 3 msg.request = 23 msg.subscription = 666 print("msg: {} {}".format(msg.request, msg.subscription)) return succeed(None)