⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module-mmap.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>7.8 mmap -- Memory-mapped file support</title>
<META NAME="description" CONTENT="7.8 mmap -- Memory-mapped file support">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-anydbm.html" tppabs="http://www.python.org/doc/current/lib/module-anydbm.html">
<LINK REL="previous" href="module-Queue.html" tppabs="http://www.python.org/doc/current/lib/module-Queue.html">
<LINK REL="up" href="someos.html" tppabs="http://www.python.org/doc/current/lib/someos.html">
<LINK REL="next" href="module-anydbm.html" tppabs="http://www.python.org/doc/current/lib/module-anydbm.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="QueueObjects.html" tppabs="http://www.python.org/doc/current/lib/QueueObjects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="someos.html" tppabs="http://www.python.org/doc/current/lib/someos.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="module-anydbm.html" tppabs="http://www.python.org/doc/current/lib/module-anydbm.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="QueueObjects.html" tppabs="http://www.python.org/doc/current/lib/QueueObjects.html">7.7.1 Queue Objects</A>
<b class="navlabel">Up:</b> <a class="sectref" href="someos.html" tppabs="http://www.python.org/doc/current/lib/someos.html">7. Optional Operating System</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-anydbm.html" tppabs="http://www.python.org/doc/current/lib/module-anydbm.html">7.9 anydbm  </A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION009800000000000000000">
7.8 <tt class="module">mmap</tt> --
	Memory-mapped file support</A>
</H1>

<P>


<P>
Memory-mapped file objects behave like both mutable strings and like
file objects.  You can use mmap objects in most places where strings
are expected; for example, you can use the <tt class="module">re</tt> module to
search through a memory-mapped file.  Since they're mutable, you can
change a single character by doing <code>obj[<var>index</var>] = 'a'</code>, or
change a substring by assigning to a slice:
<code>obj[<var>i1</var>:<var>i2</var>] = '...'</code>.  You can also read and write
data starting at the current file position, and <tt class="method">seek()</tt>
through the file to different positions.

<P>
A memory-mapped file is created by the following function, which is
different on Unix and on Windows.

<P>
<dl><dt><b><a name='l2h-1688'><tt class='function'>mmap</tt></a></b> (<var>fileno, length</var><big>[</big><var>, tagname</var><big>]</big>)
<dd>
<b>(Windows version)</b>  Maps <var>length</var> bytes from the file
specified by the file handle <var>fileno</var>, and returns a mmap object.
If you wish to map an existing Python file object, use its
<tt class="method">fileno()</tt> method to obtain the correct value for the
<var>fileno</var> parameter.

<P>
<var>tagname</var>, if specified, is a string giving a tag name for the mapping. 
Windows allows you to have many different mappings against the same
file.  If you specify the name of an existing tag, that tag is opened,
otherwise a new tag of this name is created.  If this parameter is
None, the mapping is created without a name.  Avoiding the use of the
tag parameter will assist in keeping your code portable between Unix
and Windows.
</dl>

<P>
<dl><dt><b><a name='l2h-1689'><tt class='function'>mmap</tt></a></b> (<var>fileno, size</var><big>[</big><var>, flags, prot</var><big>]</big>)
<dd>
<b>(Unix version)</b>  Maps <var>length</var> bytes from the file
specified by the file handle <var>fileno</var>, and returns a mmap object.
If you wish to map an existing Python file object, use its
<tt class="method">fileno()</tt> method to obtain the correct value for the
<var>fileno</var> parameter.

<P>
<var>flags</var> specifies the nature of the mapping.  
<tt class="constant">MAP_PRIVATE</tt> creates a private copy-on-write mapping, so
changes to the contents of the mmap object will be private to this
process, and <tt class="constant">MAP_SHARED</tt> creates a mapping that's shared
with all other processes mapping the same areas of the file.
The default value is <tt class="constant">MAP_SHARED</tt>.

<P>
<var>prot</var>, if specified, gives the desired memory protection; the two 
most useful values are <tt class="constant">PROT_READ</tt> and <tt class="constant">PROT_WRITE</tt>,
to specify that the pages may be read or written.
<var>prot</var> defaults to <tt class="constant">PROT_READ | PROT_WRITE</tt>.
</dl>

<P>
Memory-mapped file objects support the following methods:

<P>
<dl><dt><b><a name='l2h-1690'><tt class='method'>close</tt></a></b> ()
<dd>
Close the file.  Subsequent calls to other methods of the object
will result in an exception being raised.
</dl>

<P>
<dl><dt><b><a name='l2h-1691'><tt class='method'>find</tt></a></b> (<var>string</var><big>[</big><var>, start</var><big>]</big>)
<dd>
Returns the lowest index in the object where the substring
<var>string</var> is found.  Returns <code>-1</code> on failure.  <var>start</var> is
the index at which the search begins, and defaults to zero.
</dl>

<P>
<dl><dt><b><a name='l2h-1692'><tt class='method'>flush</tt></a></b> (<big>[</big><var>offset, size</var><big>]</big>)
<dd>
Flushes changes made to the in-memory copy of a file back to disk.
Without use of this call there is no guarantee that changes are
written back before the object is destroyed.  If <var>offset</var> and
<var>size</var> are specified, only changes to the given range of bytes
will be flushed to disk; otherwise, the whole extent of the mapping is
flushed.
</dl>

<P>
<dl><dt><b><a name='l2h-1693'><tt class='method'>move</tt></a></b> (<var><var>dest</var>, <var>src</var>, <var>count</var></var>)
<dd>
Copy the <var>count</var> bytes starting at offset <var>src</var> 
to the destination index <var>dest</var>.
</dl>

<P>
<dl><dt><b><a name='l2h-1694'><tt class='method'>read</tt></a></b> (<var><var>num</var></var>)
<dd>
Return a string containing up to <var>num</var> bytes starting from the
current file position; the file position is updated to point after the
bytes that were returned. 
</dl>

<P>
<dl><dt><b><a name='l2h-1695'><tt class='method'>read_byte</tt></a></b> ()
<dd>
Returns a string of length 1 containing the character at the current
file position, and advances the file position by 1.
</dl>

<P>
<dl><dt><b><a name='l2h-1696'><tt class='method'>readline</tt></a></b> ()
<dd>
Returns a single line, starting at the current file position and up to 
the next newline.
</dl>

<P>
<dl><dt><b><a name='l2h-1697'><tt class='method'>resize</tt></a></b> (<var><var>newsize</var></var>)
<dd>
</dl>

<P>
<dl><dt><b><a name='l2h-1698'><tt class='method'>seek</tt></a></b> (<var>pos</var><big>[</big><var>, whence</var><big>]</big>)
<dd>
Set the file's current position.
<var>whence</var> argument is optional and defaults to <code>0</code> (absolute
file positioning); other values are <code>1</code> (seek relative to the
current position) and <code>2</code> (seek relative to the file's end).
</dl>

<P>
<dl><dt><b><a name='l2h-1699'><tt class='method'>size</tt></a></b> ()
<dd>
Return the length of the file, which can be larger than the size
of the memory-mapped area. 
</dl>

<P>
<dl><dt><b><a name='l2h-1700'><tt class='method'>tell</tt></a></b> ()
<dd>
Returns the current position of the file pointer.
</dl>

<P>
<dl><dt><b><a name='l2h-1701'><tt class='method'>write</tt></a></b> (<var><var>string</var></var>)
<dd>
Write the bytes in <var>string</var> into memory at the current position of
the file pointer; the file position is updated to point after the
bytes that were written. 
</dl>

<P>
<dl><dt><b><a name='l2h-1702'><tt class='method'>write_byte</tt></a></b> (<var><var>byte</var></var>)
<dd>
Write the single-character string <var>byte</var> into memory at the
current position of the file pointer; the file position is advanced by
<code>1</code>.
</dl>

<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="QueueObjects.html" tppabs="http://www.python.org/doc/current/lib/QueueObjects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="someos.html" tppabs="http://www.python.org/doc/current/lib/someos.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="module-anydbm.html" tppabs="http://www.python.org/doc/current/lib/module-anydbm.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="QueueObjects.html" tppabs="http://www.python.org/doc/current/lib/QueueObjects.html">7.7.1 Queue Objects</A>
<b class="navlabel">Up:</b> <a class="sectref" href="someos.html" tppabs="http://www.python.org/doc/current/lib/someos.html">7. Optional Operating System</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-anydbm.html" tppabs="http://www.python.org/doc/current/lib/module-anydbm.html">7.9 anydbm  </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -