[issue21501] submitting mmap example for use in documentation

2016-11-06 Thread Guido van Rossum

Guido van Rossum added the comment:

Can you say it in the form of a patch?

--
nosy: +gvanrossum

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21501] submitting mmap example for use in documentation

2014-05-13 Thread Steve Foley

New submission from Steve Foley:

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)

--
assignee: docs@python
components: Documentation
messages: 218478
nosy: docs@python, hudson
priority: normal
severity: normal
status: open
title: submitting mmap example for use in documentation
type: enhancement
versions: Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21501
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue21501] submitting mmap example for use in documentation

2014-05-13 Thread Steve Foley

Steve Foley added the comment:

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)

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue21501
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com