classification
Title: docs: mmap example for use in documentation
Type: enhancement Stage:
Components: Documentation Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: dhoekstra, docs@python, gvanrossum, hudson
Priority: normal Keywords: easy

Created on 2014-05-13 19:18 by hudson, last changed 2021-05-11 09:48 by dhoekstra.

Messages (4)
msg218478 - (view) Author: Steve Foley (hudson) Date: 2014-05-13 19:18
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)
msg218483 - (view) Author: Steve Foley (hudson) Date: 2014-05-13 20:13
sorry! this is the correct version ;-)

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

import mmap, os, select

NUM_CHILDREN = 30
MSG_LEN = 9
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):
    msgs = NUM_CHILDREN
    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)
msg280148 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-11-06 16:40
Can you say it in the form of a patch?
msg393449 - (view) Author: Douwe Hoekstra (dhoekstra) * Date: 2021-05-11 09:48
Hi! I'd like to convert this to reST for the documentation. Additionally,
I think this example would benefit from additional explanation about how it functions.
History
Date User Action Args
2021-05-11 09:48:51dhoekstrasetnosy: + dhoekstra
messages: + msg393449
2021-05-10 21:48:51iritkatrielsetkeywords: + easy
title: submitting mmap example for use in documentation -> docs: mmap example for use in documentation
versions: + Python 3.11, - Python 2.7
2016-11-06 16:40:20gvanrossumsetnosy: + gvanrossum
messages: + msg280148
2014-05-13 20:13:23hudsonsetmessages: + msg218483
2014-05-13 19:18:53hudsoncreate