summaryrefslogtreecommitdiffstats
path: root/Doc/library/mmap.rst
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2007-12-02 15:22:16 (GMT)
committerChristian Heimes <christian@cheimes.de>2007-12-02 15:22:16 (GMT)
commitd8654cf758c730af430026a5b20810bebceba4d7 (patch)
treecba77a4cce70773ed2bf00d183459101c53dfd26 /Doc/library/mmap.rst
parentb27ce7e46843841e8e8f2c9e5108044d022ae248 (diff)
downloadcpython-d8654cf758c730af430026a5b20810bebceba4d7.zip
cpython-d8654cf758c730af430026a5b20810bebceba4d7.tar.gz
cpython-d8654cf758c730af430026a5b20810bebceba4d7.tar.bz2
Merged revisions 59259-59274 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r59260 | lars.gustaebel | 2007-12-01 22:02:12 +0100 (Sat, 01 Dec 2007) | 5 lines Issue #1531: Read fileobj from the current offset, do not seek to the start. (will backport to 2.5) ........ r59262 | georg.brandl | 2007-12-01 23:24:47 +0100 (Sat, 01 Dec 2007) | 4 lines Document PyEval_* functions from ceval.c. Credits to Michael Sloan from GHOP. ........ r59263 | georg.brandl | 2007-12-01 23:27:56 +0100 (Sat, 01 Dec 2007) | 2 lines Add a few refcount data entries. ........ r59264 | georg.brandl | 2007-12-01 23:38:48 +0100 (Sat, 01 Dec 2007) | 4 lines Add test suite for cmd module. Written by Michael Schneider for GHOP. ........ r59265 | georg.brandl | 2007-12-01 23:42:46 +0100 (Sat, 01 Dec 2007) | 3 lines Add examples to the ElementTree documentation. Written by h4wk.cz for GHOP. ........ r59266 | georg.brandl | 2007-12-02 00:12:45 +0100 (Sun, 02 Dec 2007) | 3 lines Add "Using Python on Windows" document, by Robert Lehmann. Written for GHOP. ........ r59271 | georg.brandl | 2007-12-02 15:34:34 +0100 (Sun, 02 Dec 2007) | 3 lines Add example to mmap docs. Written for GHOP by Rafal Rawicki. ........ r59272 | georg.brandl | 2007-12-02 15:37:29 +0100 (Sun, 02 Dec 2007) | 2 lines Convert bdb.rst line endings to Unix style. ........ r59274 | georg.brandl | 2007-12-02 15:58:50 +0100 (Sun, 02 Dec 2007) | 4 lines Add more entries to the glossary. Written by Jeff Wheeler for GHOP. ........
Diffstat (limited to 'Doc/library/mmap.rst')
-rw-r--r--Doc/library/mmap.rst43
1 files changed, 43 insertions, 0 deletions
diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst
index 26748c4..715610e 100644
--- a/Doc/library/mmap.rst
+++ b/Doc/library/mmap.rst
@@ -84,6 +84,49 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
*offset* may be specified as a non-negative integer offset. mmap references will
be relative to the offset from the beginning of the file. *offset* defaults to 0.
*offset* must be a multiple of the PAGESIZE or ALLOCATIONGRANULARITY.
+
+ This example shows a simple way of using :func:`mmap`::
+
+ import mmap
+
+ # write a simple example file
+ with open("hello.txt", "w") as f:
+ f.write("Hello Python!\n")
+
+ with open("hello.txt", "r+") as f:
+ # memory-map the file, size 0 means whole file
+ map = mmap.mmap(f.fileno(), 0)
+ # read content via standard file methods
+ print map.readline() # prints "Hello Python!"
+ # read content via slice notation
+ print map[:5] # prints "Hello"
+ # update content using slice notation;
+ # note that new content must have same size
+ map[6:] = " world!\n"
+ # ... and read again using standard file methods
+ map.seek(0)
+ print map.readline() # prints "Hello world!"
+ # close the map
+ map.close()
+
+
+ The next example demonstrates how to create an anonymous map and exchange
+ data between the parent and child processes::
+
+ import mmap
+ import os
+
+ map = mmap.mmap(-1, 13)
+ map.write("Hello world!")
+
+ pid = os.fork()
+
+ if pid == 0: # In a child process
+ map.seek(0)
+ print map.readline()
+
+ map.close()
+
Memory-mapped file objects support the following methods: