Author hudson
Recipients docs@python, hudson
Date 2014-05-13.19:18:52
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1400008733.4.0.146542624997.issue21501@psf.upfronthosting.co.za>
In-reply-to
Content
Hello, I would like to submit an example for the mmap docs page. It demonstrates the use of shared memory and message passing between processes. Thanks!

--------------------

import mmap, os, select

NUM_CHILDREN = 30
MSG_LEN = 8
BUF_LEN = NUM_CHILDREN * MSG_LEN

buf = mmap.mmap(-1, BUF_LEN)
p = select.poll()

def write_buffer(i):
    msg = '%s\t%d\n' % (i, os.getpid())
    offset = MSG_LEN * i
    buf.seek(offset)
    buf.write(msg)

def child(i, pipeout):
    write_buffer(i)
    os.write(pipeout, 'OK\0'.encode())
    os._exit(0)

def fork(i, p):
    pipein, pipeout = os.pipe()
    if os.fork() == 0:
        child(i, pipeout)
    else:
        p.register(pipein)

def loop(msgs, p):
    while msgs:
        for fd, event in p.poll():
            p.unregister(fd)
            msgs = msgs - 1

for i in range(NUM_CHILDREN):
    fork(i, p)

loop(NUM_CHILDREN, p)

buf.seek(0)
print buf.read(BUF_LEN)
History
Date User Action Args
2014-05-13 19:18:53hudsonsetrecipients: + hudson, docs@python
2014-05-13 19:18:53hudsonsetmessageid: <1400008733.4.0.146542624997.issue21501@psf.upfronthosting.co.za>
2014-05-13 19:18:53hudsonlinkissue21501 messages
2014-05-13 19:18:52hudsoncreate