This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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

Created on 2014-05-13 19:18 by hudson, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)
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.
msg406090 - (view) Author: mike mcleod (mikecmcleod) * Date: 2021-11-10 12:36
I would like to help with this issue.
I note that a few changes need to be made to the code example for compatibility for the latest version of Python. I can get this working but I am not sure this is a good example. Also, the example is more about threads and messaging between threads rather than mmap.
So my question is is this a good example?
And the suggestion of adding detailed explanation of the code, again, as to what it does not seem to be that relevant to mmap.
msg406106 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-11-10 15:54
We would need the OP to sign a CLA in order to be able to use (a modified version of) his code.

I presume this page is where the example was supposed to go?
https://docs.python.org/3/library/mmap.html

Let me ask first -- does that page really need another example? If so, is the OP's example (made working) really the best one?

Perhaps there are blog posts that cover how to use mmap already, and the reference docs don't need such an extensive example?  In that case we could just close this (ancient) issue.
msg406111 - (view) Author: mike mcleod (mikecmcleod) * Date: 2021-11-10 16:54
One of the things I did when the example code didn't work was to see what other examples could be found and there are I would argue plenty or just enough, eg:
https://medium.com/analytics-vidhya/memory-mapping-files-and-mmap-module-in-python-with-lot-of-examples-d1a9a45fe9a3
And google will fetch more if needed.
May I suggest back in 2014 when the issue was raised that this may have been a good example, but mmap is not so new and in the intervening time anybody who needed further examples simply googled them. Hence, I suggest its not needed.
msg406115 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-11-10 16:57
Thanks for the research! I will close the issue now.
History
Date User Action Args
2022-04-11 14:58:03adminsetgithub: 65700
2021-11-10 16:57:52gvanrossumsetstatus: open -> closed
resolution: out of date
messages: + msg406115

stage: resolved
2021-11-10 16:54:27mikecmcleodsetmessages: + msg406111
2021-11-10 15:54:40gvanrossumsetmessages: + msg406106
2021-11-10 12:36:20mikecmcleodsetnosy: + mikecmcleod
messages: + msg406090
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