gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

756 lines
61KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="mmap — Memory-mapped file support" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/mmap.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Availability: not Emscripten, not WASI. This module does not work or is not available on WebAssembly platforms wasm32-emscripten and wasm32-wasi. See WebAssembly platforms for more information. Mem..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Availability: not Emscripten, not WASI. This module does not work or is not available on WebAssembly platforms wasm32-emscripten and wasm32-wasi. See WebAssembly platforms for more information. Mem..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>mmap — Memory-mapped file support &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Internet Data Handling" href="netdata.html" />
  33. <link rel="prev" title="signal — Set handlers for asynchronous events" href="signal.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/mmap.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmap</span></code> — Memory-mapped file support</a><ul>
  86. <li><a class="reference internal" href="#madv-constants">MADV_* Constants</a></li>
  87. <li><a class="reference internal" href="#map-constants">MAP_* Constants</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="signal.html"
  95. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code> — Set handlers for asynchronous events</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="netdata.html"
  100. title="next chapter">Internet Data Handling</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/library/mmap.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="netdata.html" title="Internet Data Handling"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="signal.html" title="signal — Set handlers for asynchronous events"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="ipc.html" accesskey="U">Networking and Interprocess Communication</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmap</span></code> — Memory-mapped file support</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="module-mmap">
  170. <span id="mmap-memory-mapped-file-support"></span><h1><a class="reference internal" href="#module-mmap" title="mmap: Interface to memory-mapped files for Unix and Windows."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmap</span></code></a> — Memory-mapped file support<a class="headerlink" href="#module-mmap" title="Link to this heading">¶</a></h1>
  171. <hr class="docutils" />
  172. <div class="availability docutils container">
  173. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  174. <p>This module does not work or is not available on WebAssembly platforms
  175. <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>. See
  176. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  177. </div>
  178. <p>Memory-mapped file objects behave like both <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> and like
  179. <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file objects</span></a>. You can use mmap objects in most places
  180. where <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> are expected; for example, you can use the <a class="reference internal" href="re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a>
  181. module to search through a memory-mapped file. You can also change a single
  182. byte by doing <code class="docutils literal notranslate"><span class="pre">obj[index]</span> <span class="pre">=</span> <span class="pre">97</span></code>, or change a subsequence by assigning to a
  183. slice: <code class="docutils literal notranslate"><span class="pre">obj[i1:i2]</span> <span class="pre">=</span> <span class="pre">b'...'</span></code>. You can also read and write data starting at
  184. the current file position, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">seek()</span></code> through the file to different positions.</p>
  185. <p>A memory-mapped file is created by the <a class="reference internal" href="#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap</span></code></a> constructor, which is
  186. different on Unix and on Windows. In either case you must provide a file
  187. descriptor for a file opened for update. If you wish to map an existing Python
  188. file object, use its <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a> method to obtain the correct value for the
  189. <em>fileno</em> parameter. Otherwise, you can open the file using the
  190. <a class="reference internal" href="os.html#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a> function, which returns a file descriptor directly (the file
  191. still needs to be closed when done).</p>
  192. <div class="admonition note">
  193. <p class="admonition-title">Note</p>
  194. <p>If you want to create a memory-mapping for a writable, buffered file, you
  195. should <a class="reference internal" href="io.html#io.IOBase.flush" title="io.IOBase.flush"><code class="xref py py-func docutils literal notranslate"><span class="pre">flush()</span></code></a> the file first. This is necessary to ensure
  196. that local modifications to the buffers are actually available to the
  197. mapping.</p>
  198. </div>
  199. <p>For both the Unix and Windows versions of the constructor, <em>access</em> may be
  200. specified as an optional keyword parameter. <em>access</em> accepts one of four
  201. values: <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_WRITE</span></code>, or <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_COPY</span></code> to
  202. specify read-only, write-through or copy-on-write memory respectively, or
  203. <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_DEFAULT</span></code> to defer to <em>prot</em>. <em>access</em> can be used on both Unix
  204. and Windows. If <em>access</em> is not specified, Windows mmap returns a
  205. write-through mapping. The initial memory values for all three access types
  206. are taken from the specified file. Assignment to an <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code>
  207. memory map raises a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception. Assignment to an
  208. <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_WRITE</span></code> memory map affects both memory and the underlying file.
  209. Assignment to an <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_COPY</span></code> memory map affects memory but does not
  210. update the underlying file.</p>
  211. <div class="versionchanged">
  212. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_DEFAULT</span></code> constant.</p>
  213. </div>
  214. <p>To map anonymous memory, -1 should be passed as the fileno along with the length.</p>
  215. <dl class="py class">
  216. <dt class="sig sig-object py" id="mmap.mmap">
  217. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">mmap</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fileno</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">length</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">tagname=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">access=ACCESS_DEFAULT</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap" title="Link to this definition">¶</a></dt>
  218. <dd><p><strong>(Windows version)</strong> Maps <em>length</em> bytes from the file specified by the
  219. file handle <em>fileno</em>, and creates a mmap object. If <em>length</em> is larger
  220. than the current size of the file, the file is extended to contain <em>length</em>
  221. bytes. If <em>length</em> is <code class="docutils literal notranslate"><span class="pre">0</span></code>, the maximum length of the map is the current
  222. size of the file, except that if the file is empty Windows raises an
  223. exception (you cannot create an empty mapping on Windows).</p>
  224. <p><em>tagname</em>, if specified and not <code class="docutils literal notranslate"><span class="pre">None</span></code>, is a string giving a tag name for
  225. the mapping. Windows allows you to have many different mappings against
  226. the same file. If you specify the name of an existing tag, that tag is
  227. opened, otherwise a new tag of this name is created. If this parameter is
  228. omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>, the mapping is created without a name. Avoiding the
  229. use of the <em>tagname</em> parameter will assist in keeping your code portable
  230. between Unix and Windows.</p>
  231. <p><em>offset</em> may be specified as a non-negative integer offset. mmap references
  232. will be relative to the offset from the beginning of the file. <em>offset</em>
  233. defaults to 0. <em>offset</em> must be a multiple of the <code class="xref py py-const docutils literal notranslate"><span class="pre">ALLOCATIONGRANULARITY</span></code>.</p>
  234. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">mmap.__new__</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">fileno</span></code>, <code class="docutils literal notranslate"><span class="pre">length</span></code>, <code class="docutils literal notranslate"><span class="pre">access</span></code>, <code class="docutils literal notranslate"><span class="pre">offset</span></code>.</p>
  235. </dd></dl>
  236. <dl class="py class">
  237. <dt class="sig sig-object py">
  238. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">mmap</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fileno</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">length</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">flags=MAP_SHARED</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prot=PROT_WRITE|PROT_READ</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">access=ACCESS_DEFAULT</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">offset</span></span></em><span class="optional">]</span><span class="sig-paren">)</span></dt>
  239. <dd><p><strong>(Unix version)</strong> Maps <em>length</em> bytes from the file specified by the file
  240. descriptor <em>fileno</em>, and returns a mmap object. If <em>length</em> is <code class="docutils literal notranslate"><span class="pre">0</span></code>, the
  241. maximum length of the map will be the current size of the file when
  242. <a class="reference internal" href="#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap</span></code></a> is called.</p>
  243. <p><em>flags</em> specifies the nature of the mapping. <a class="reference internal" href="#mmap.MAP_PRIVATE" title="mmap.MAP_PRIVATE"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAP_PRIVATE</span></code></a> creates a
  244. private copy-on-write mapping, so changes to the contents of the mmap
  245. object will be private to this process, and <a class="reference internal" href="#mmap.MAP_SHARED" title="mmap.MAP_SHARED"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAP_SHARED</span></code></a> creates a
  246. mapping that’s shared with all other processes mapping the same areas of
  247. the file. The default value is <a class="reference internal" href="#mmap.MAP_SHARED" title="mmap.MAP_SHARED"><code class="xref py py-const docutils literal notranslate"><span class="pre">MAP_SHARED</span></code></a>. Some systems have
  248. additional possible flags with the full list specified in
  249. <a class="reference internal" href="#map-constants"><span class="std std-ref">MAP_* constants</span></a>.</p>
  250. <p><em>prot</em>, if specified, gives the desired memory protection; the two most
  251. useful values are <code class="xref py py-const docutils literal notranslate"><span class="pre">PROT_READ</span></code> and <code class="xref py py-const docutils literal notranslate"><span class="pre">PROT_WRITE</span></code>, to specify
  252. that the pages may be read or written. <em>prot</em> defaults to
  253. <code class="xref py py-const docutils literal notranslate"><span class="pre">PROT_READ</span> <span class="pre">|</span> <span class="pre">PROT_WRITE</span></code>.</p>
  254. <p><em>access</em> may be specified in lieu of <em>flags</em> and <em>prot</em> as an optional
  255. keyword parameter. It is an error to specify both <em>flags</em>, <em>prot</em> and
  256. <em>access</em>. See the description of <em>access</em> above for information on how to
  257. use this parameter.</p>
  258. <p><em>offset</em> may be specified as a non-negative integer offset. mmap references
  259. will be relative to the offset from the beginning of the file. <em>offset</em>
  260. defaults to 0. <em>offset</em> must be a multiple of <code class="xref py py-const docutils literal notranslate"><span class="pre">ALLOCATIONGRANULARITY</span></code>
  261. which is equal to <code class="xref py py-const docutils literal notranslate"><span class="pre">PAGESIZE</span></code> on Unix systems.</p>
  262. <p>To ensure validity of the created memory mapping the file specified
  263. by the descriptor <em>fileno</em> is internally automatically synchronized
  264. with the physical backing store on macOS.</p>
  265. <p>This example shows a simple way of using <a class="reference internal" href="#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap</span></code></a>:</p>
  266. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mmap</span>
  267. <span class="c1"># write a simple example file</span>
  268. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;hello.txt&quot;</span><span class="p">,</span> <span class="s2">&quot;wb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  269. <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Hello Python!</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
  270. <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;hello.txt&quot;</span><span class="p">,</span> <span class="s2">&quot;r+b&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  271. <span class="c1"># memory-map the file, size 0 means whole file</span>
  272. <span class="n">mm</span> <span class="o">=</span> <span class="n">mmap</span><span class="o">.</span><span class="n">mmap</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="mi">0</span><span class="p">)</span>
  273. <span class="c1"># read content via standard file methods</span>
  274. <span class="nb">print</span><span class="p">(</span><span class="n">mm</span><span class="o">.</span><span class="n">readline</span><span class="p">())</span> <span class="c1"># prints b&quot;Hello Python!\n&quot;</span>
  275. <span class="c1"># read content via slice notation</span>
  276. <span class="nb">print</span><span class="p">(</span><span class="n">mm</span><span class="p">[:</span><span class="mi">5</span><span class="p">])</span> <span class="c1"># prints b&quot;Hello&quot;</span>
  277. <span class="c1"># update content using slice notation;</span>
  278. <span class="c1"># note that new content must have same size</span>
  279. <span class="n">mm</span><span class="p">[</span><span class="mi">6</span><span class="p">:]</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot; world!</span><span class="se">\n</span><span class="s2">&quot;</span>
  280. <span class="c1"># ... and read again using standard file methods</span>
  281. <span class="n">mm</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  282. <span class="nb">print</span><span class="p">(</span><span class="n">mm</span><span class="o">.</span><span class="n">readline</span><span class="p">())</span> <span class="c1"># prints b&quot;Hello world!\n&quot;</span>
  283. <span class="c1"># close the map</span>
  284. <span class="n">mm</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  285. </pre></div>
  286. </div>
  287. <p><a class="reference internal" href="#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap</span></code></a> can also be used as a context manager in a <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a>
  288. statement:</p>
  289. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mmap</span>
  290. <span class="k">with</span> <span class="n">mmap</span><span class="o">.</span><span class="n">mmap</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">13</span><span class="p">)</span> <span class="k">as</span> <span class="n">mm</span><span class="p">:</span>
  291. <span class="n">mm</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Hello world!&quot;</span><span class="p">)</span>
  292. </pre></div>
  293. </div>
  294. <div class="versionadded">
  295. <p><span class="versionmodified added">New in version 3.2: </span>Context manager support.</p>
  296. </div>
  297. <p>The next example demonstrates how to create an anonymous map and exchange
  298. data between the parent and child processes:</p>
  299. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mmap</span>
  300. <span class="kn">import</span> <span class="nn">os</span>
  301. <span class="n">mm</span> <span class="o">=</span> <span class="n">mmap</span><span class="o">.</span><span class="n">mmap</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">13</span><span class="p">)</span>
  302. <span class="n">mm</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Hello world!&quot;</span><span class="p">)</span>
  303. <span class="n">pid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">fork</span><span class="p">()</span>
  304. <span class="k">if</span> <span class="n">pid</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="c1"># In a child process</span>
  305. <span class="n">mm</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  306. <span class="nb">print</span><span class="p">(</span><span class="n">mm</span><span class="o">.</span><span class="n">readline</span><span class="p">())</span>
  307. <span class="n">mm</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  308. </pre></div>
  309. </div>
  310. <p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">mmap.__new__</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">fileno</span></code>, <code class="docutils literal notranslate"><span class="pre">length</span></code>, <code class="docutils literal notranslate"><span class="pre">access</span></code>, <code class="docutils literal notranslate"><span class="pre">offset</span></code>.</p>
  311. <p>Memory-mapped file objects support the following methods:</p>
  312. <dl class="py method">
  313. <dt class="sig sig-object py" id="mmap.mmap.close">
  314. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.close" title="Link to this definition">¶</a></dt>
  315. <dd><p>Closes the mmap. Subsequent calls to other methods of the object will
  316. result in a ValueError exception being raised. This will not close
  317. the open file.</p>
  318. </dd></dl>
  319. <dl class="py attribute">
  320. <dt class="sig sig-object py" id="mmap.mmap.closed">
  321. <span class="sig-name descname"><span class="pre">closed</span></span><a class="headerlink" href="#mmap.mmap.closed" title="Link to this definition">¶</a></dt>
  322. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the file is closed.</p>
  323. <div class="versionadded">
  324. <p><span class="versionmodified added">New in version 3.2.</span></p>
  325. </div>
  326. </dd></dl>
  327. <dl class="py method">
  328. <dt class="sig sig-object py" id="mmap.mmap.find">
  329. <span class="sig-name descname"><span class="pre">find</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sub</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">start</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">end</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.find" title="Link to this definition">¶</a></dt>
  330. <dd><p>Returns the lowest index in the object where the subsequence <em>sub</em> is
  331. found, such that <em>sub</em> is contained in the range [<em>start</em>, <em>end</em>].
  332. Optional arguments <em>start</em> and <em>end</em> are interpreted as in slice notation.
  333. Returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> on failure.</p>
  334. <div class="versionchanged">
  335. <p><span class="versionmodified changed">Changed in version 3.5: </span>Writable <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> is now accepted.</p>
  336. </div>
  337. </dd></dl>
  338. <dl class="py method">
  339. <dt class="sig sig-object py" id="mmap.mmap.flush">
  340. <span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">offset</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">size</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.flush" title="Link to this definition">¶</a></dt>
  341. <dd><p>Flushes changes made to the in-memory copy of a file back to disk. Without
  342. use of this call there is no guarantee that changes are written back before
  343. the object is destroyed. If <em>offset</em> and <em>size</em> are specified, only
  344. changes to the given range of bytes will be flushed to disk; otherwise, the
  345. whole extent of the mapping is flushed. <em>offset</em> must be a multiple of the
  346. <code class="xref py py-const docutils literal notranslate"><span class="pre">PAGESIZE</span></code> or <code class="xref py py-const docutils literal notranslate"><span class="pre">ALLOCATIONGRANULARITY</span></code>.</p>
  347. <p><code class="docutils literal notranslate"><span class="pre">None</span></code> is returned to indicate success. An exception is raised when the
  348. call failed.</p>
  349. <div class="versionchanged">
  350. <p><span class="versionmodified changed">Changed in version 3.8: </span>Previously, a nonzero value was returned on success; zero was returned
  351. on error under Windows. A zero value was returned on success; an
  352. exception was raised on error under Unix.</p>
  353. </div>
  354. </dd></dl>
  355. <dl class="py method">
  356. <dt class="sig sig-object py" id="mmap.mmap.madvise">
  357. <span class="sig-name descname"><span class="pre">madvise</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">option</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">start</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">length</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.madvise" title="Link to this definition">¶</a></dt>
  358. <dd><p>Send advice <em>option</em> to the kernel about the memory region beginning at
  359. <em>start</em> and extending <em>length</em> bytes. <em>option</em> must be one of the
  360. <a class="reference internal" href="#madvise-constants"><span class="std std-ref">MADV_* constants</span></a> available on the system. If
  361. <em>start</em> and <em>length</em> are omitted, the entire mapping is spanned. On
  362. some systems (including Linux), <em>start</em> must be a multiple of the
  363. <code class="xref py py-const docutils literal notranslate"><span class="pre">PAGESIZE</span></code>.</p>
  364. <p>Availability: Systems with the <code class="docutils literal notranslate"><span class="pre">madvise()</span></code> system call.</p>
  365. <div class="versionadded">
  366. <p><span class="versionmodified added">New in version 3.8.</span></p>
  367. </div>
  368. </dd></dl>
  369. <dl class="py method">
  370. <dt class="sig sig-object py" id="mmap.mmap.move">
  371. <span class="sig-name descname"><span class="pre">move</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dest</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">src</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">count</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.move" title="Link to this definition">¶</a></dt>
  372. <dd><p>Copy the <em>count</em> bytes starting at offset <em>src</em> to the destination index
  373. <em>dest</em>. If the mmap was created with <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code>, then calls to
  374. move will raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception.</p>
  375. </dd></dl>
  376. <dl class="py method">
  377. <dt class="sig sig-object py" id="mmap.mmap.read">
  378. <span class="sig-name descname"><span class="pre">read</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.read" title="Link to this definition">¶</a></dt>
  379. <dd><p>Return a <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> containing up to <em>n</em> bytes starting from the
  380. current file position. If the argument is omitted, <code class="docutils literal notranslate"><span class="pre">None</span></code> or negative,
  381. return all bytes from the current file position to the end of the
  382. mapping. The file position is updated to point after the bytes that were
  383. returned.</p>
  384. <div class="versionchanged">
  385. <p><span class="versionmodified changed">Changed in version 3.3: </span>Argument can be omitted or <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  386. </div>
  387. </dd></dl>
  388. <dl class="py method">
  389. <dt class="sig sig-object py" id="mmap.mmap.read_byte">
  390. <span class="sig-name descname"><span class="pre">read_byte</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.read_byte" title="Link to this definition">¶</a></dt>
  391. <dd><p>Returns a byte at the current file position as an integer, and advances
  392. the file position by 1.</p>
  393. </dd></dl>
  394. <dl class="py method">
  395. <dt class="sig sig-object py" id="mmap.mmap.readline">
  396. <span class="sig-name descname"><span class="pre">readline</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.readline" title="Link to this definition">¶</a></dt>
  397. <dd><p>Returns a single line, starting at the current file position and up to the
  398. next newline. The file position is updated to point after the bytes that were
  399. returned.</p>
  400. </dd></dl>
  401. <dl class="py method">
  402. <dt class="sig sig-object py" id="mmap.mmap.resize">
  403. <span class="sig-name descname"><span class="pre">resize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">newsize</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.resize" title="Link to this definition">¶</a></dt>
  404. <dd><p>Resizes the map and the underlying file, if any. If the mmap was created
  405. with <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code> or <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_COPY</span></code>, resizing the map will
  406. raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception.</p>
  407. <p><strong>On Windows</strong>: Resizing the map will raise an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if there are other
  408. maps against the same named file. Resizing an anonymous map (ie against the
  409. pagefile) will silently create a new map with the original data copied over
  410. up to the length of the new size.</p>
  411. <div class="versionchanged">
  412. <p><span class="versionmodified changed">Changed in version 3.11: </span>Correctly fails if attempting to resize when another map is held
  413. Allows resize against an anonymous map on Windows</p>
  414. </div>
  415. </dd></dl>
  416. <dl class="py method">
  417. <dt class="sig sig-object py" id="mmap.mmap.rfind">
  418. <span class="sig-name descname"><span class="pre">rfind</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sub</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">start</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">end</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.rfind" title="Link to this definition">¶</a></dt>
  419. <dd><p>Returns the highest index in the object where the subsequence <em>sub</em> is
  420. found, such that <em>sub</em> is contained in the range [<em>start</em>, <em>end</em>].
  421. Optional arguments <em>start</em> and <em>end</em> are interpreted as in slice notation.
  422. Returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> on failure.</p>
  423. <div class="versionchanged">
  424. <p><span class="versionmodified changed">Changed in version 3.5: </span>Writable <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> is now accepted.</p>
  425. </div>
  426. </dd></dl>
  427. <dl class="py method">
  428. <dt class="sig sig-object py" id="mmap.mmap.seek">
  429. <span class="sig-name descname"><span class="pre">seek</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pos</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">whence</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.seek" title="Link to this definition">¶</a></dt>
  430. <dd><p>Set the file’s current position. <em>whence</em> argument is optional and
  431. defaults to <code class="docutils literal notranslate"><span class="pre">os.SEEK_SET</span></code> or <code class="docutils literal notranslate"><span class="pre">0</span></code> (absolute file positioning); other
  432. values are <code class="docutils literal notranslate"><span class="pre">os.SEEK_CUR</span></code> or <code class="docutils literal notranslate"><span class="pre">1</span></code> (seek relative to the current
  433. position) and <code class="docutils literal notranslate"><span class="pre">os.SEEK_END</span></code> or <code class="docutils literal notranslate"><span class="pre">2</span></code> (seek relative to the file’s end).</p>
  434. </dd></dl>
  435. <dl class="py method">
  436. <dt class="sig sig-object py" id="mmap.mmap.size">
  437. <span class="sig-name descname"><span class="pre">size</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.size" title="Link to this definition">¶</a></dt>
  438. <dd><p>Return the length of the file, which can be larger than the size of the
  439. memory-mapped area.</p>
  440. </dd></dl>
  441. <dl class="py method">
  442. <dt class="sig sig-object py" id="mmap.mmap.tell">
  443. <span class="sig-name descname"><span class="pre">tell</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.tell" title="Link to this definition">¶</a></dt>
  444. <dd><p>Returns the current position of the file pointer.</p>
  445. </dd></dl>
  446. <dl class="py method">
  447. <dt class="sig sig-object py" id="mmap.mmap.write">
  448. <span class="sig-name descname"><span class="pre">write</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">bytes</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.write" title="Link to this definition">¶</a></dt>
  449. <dd><p>Write the bytes in <em>bytes</em> into memory at the current position of the
  450. file pointer and return the number of bytes written (never less than
  451. <code class="docutils literal notranslate"><span class="pre">len(bytes)</span></code>, since if the write fails, a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> will be
  452. raised). The file position is updated to point after the bytes that
  453. were written. If the mmap was created with <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code>, then
  454. writing to it will raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception.</p>
  455. <div class="versionchanged">
  456. <p><span class="versionmodified changed">Changed in version 3.5: </span>Writable <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> is now accepted.</p>
  457. </div>
  458. <div class="versionchanged">
  459. <p><span class="versionmodified changed">Changed in version 3.6: </span>The number of bytes written is now returned.</p>
  460. </div>
  461. </dd></dl>
  462. <dl class="py method">
  463. <dt class="sig sig-object py" id="mmap.mmap.write_byte">
  464. <span class="sig-name descname"><span class="pre">write_byte</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">byte</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mmap.mmap.write_byte" title="Link to this definition">¶</a></dt>
  465. <dd><p>Write the integer <em>byte</em> into memory at the current
  466. position of the file pointer; the file position is advanced by <code class="docutils literal notranslate"><span class="pre">1</span></code>. If
  467. the mmap was created with <code class="xref py py-const docutils literal notranslate"><span class="pre">ACCESS_READ</span></code>, then writing to it will
  468. raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception.</p>
  469. </dd></dl>
  470. </dd></dl>
  471. <section id="madv-constants">
  472. <span id="madvise-constants"></span><h2>MADV_* Constants<a class="headerlink" href="#madv-constants" title="Link to this heading">¶</a></h2>
  473. <dl class="py data">
  474. <dt class="sig sig-object py" id="mmap.MADV_NORMAL">
  475. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_NORMAL</span></span><a class="headerlink" href="#mmap.MADV_NORMAL" title="Link to this definition">¶</a></dt>
  476. <dt class="sig sig-object py" id="mmap.MADV_RANDOM">
  477. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_RANDOM</span></span><a class="headerlink" href="#mmap.MADV_RANDOM" title="Link to this definition">¶</a></dt>
  478. <dt class="sig sig-object py" id="mmap.MADV_SEQUENTIAL">
  479. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_SEQUENTIAL</span></span><a class="headerlink" href="#mmap.MADV_SEQUENTIAL" title="Link to this definition">¶</a></dt>
  480. <dt class="sig sig-object py" id="mmap.MADV_WILLNEED">
  481. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_WILLNEED</span></span><a class="headerlink" href="#mmap.MADV_WILLNEED" title="Link to this definition">¶</a></dt>
  482. <dt class="sig sig-object py" id="mmap.MADV_DONTNEED">
  483. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_DONTNEED</span></span><a class="headerlink" href="#mmap.MADV_DONTNEED" title="Link to this definition">¶</a></dt>
  484. <dt class="sig sig-object py" id="mmap.MADV_REMOVE">
  485. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_REMOVE</span></span><a class="headerlink" href="#mmap.MADV_REMOVE" title="Link to this definition">¶</a></dt>
  486. <dt class="sig sig-object py" id="mmap.MADV_DONTFORK">
  487. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_DONTFORK</span></span><a class="headerlink" href="#mmap.MADV_DONTFORK" title="Link to this definition">¶</a></dt>
  488. <dt class="sig sig-object py" id="mmap.MADV_DOFORK">
  489. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_DOFORK</span></span><a class="headerlink" href="#mmap.MADV_DOFORK" title="Link to this definition">¶</a></dt>
  490. <dt class="sig sig-object py" id="mmap.MADV_HWPOISON">
  491. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_HWPOISON</span></span><a class="headerlink" href="#mmap.MADV_HWPOISON" title="Link to this definition">¶</a></dt>
  492. <dt class="sig sig-object py" id="mmap.MADV_MERGEABLE">
  493. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_MERGEABLE</span></span><a class="headerlink" href="#mmap.MADV_MERGEABLE" title="Link to this definition">¶</a></dt>
  494. <dt class="sig sig-object py" id="mmap.MADV_UNMERGEABLE">
  495. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_UNMERGEABLE</span></span><a class="headerlink" href="#mmap.MADV_UNMERGEABLE" title="Link to this definition">¶</a></dt>
  496. <dt class="sig sig-object py" id="mmap.MADV_SOFT_OFFLINE">
  497. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_SOFT_OFFLINE</span></span><a class="headerlink" href="#mmap.MADV_SOFT_OFFLINE" title="Link to this definition">¶</a></dt>
  498. <dt class="sig sig-object py" id="mmap.MADV_HUGEPAGE">
  499. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_HUGEPAGE</span></span><a class="headerlink" href="#mmap.MADV_HUGEPAGE" title="Link to this definition">¶</a></dt>
  500. <dt class="sig sig-object py" id="mmap.MADV_NOHUGEPAGE">
  501. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_NOHUGEPAGE</span></span><a class="headerlink" href="#mmap.MADV_NOHUGEPAGE" title="Link to this definition">¶</a></dt>
  502. <dt class="sig sig-object py" id="mmap.MADV_DONTDUMP">
  503. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_DONTDUMP</span></span><a class="headerlink" href="#mmap.MADV_DONTDUMP" title="Link to this definition">¶</a></dt>
  504. <dt class="sig sig-object py" id="mmap.MADV_DODUMP">
  505. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_DODUMP</span></span><a class="headerlink" href="#mmap.MADV_DODUMP" title="Link to this definition">¶</a></dt>
  506. <dt class="sig sig-object py" id="mmap.MADV_FREE">
  507. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_FREE</span></span><a class="headerlink" href="#mmap.MADV_FREE" title="Link to this definition">¶</a></dt>
  508. <dt class="sig sig-object py" id="mmap.MADV_NOSYNC">
  509. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_NOSYNC</span></span><a class="headerlink" href="#mmap.MADV_NOSYNC" title="Link to this definition">¶</a></dt>
  510. <dt class="sig sig-object py" id="mmap.MADV_AUTOSYNC">
  511. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_AUTOSYNC</span></span><a class="headerlink" href="#mmap.MADV_AUTOSYNC" title="Link to this definition">¶</a></dt>
  512. <dt class="sig sig-object py" id="mmap.MADV_NOCORE">
  513. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_NOCORE</span></span><a class="headerlink" href="#mmap.MADV_NOCORE" title="Link to this definition">¶</a></dt>
  514. <dt class="sig sig-object py" id="mmap.MADV_CORE">
  515. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_CORE</span></span><a class="headerlink" href="#mmap.MADV_CORE" title="Link to this definition">¶</a></dt>
  516. <dt class="sig sig-object py" id="mmap.MADV_PROTECT">
  517. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_PROTECT</span></span><a class="headerlink" href="#mmap.MADV_PROTECT" title="Link to this definition">¶</a></dt>
  518. <dt class="sig sig-object py" id="mmap.MADV_FREE_REUSABLE">
  519. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_FREE_REUSABLE</span></span><a class="headerlink" href="#mmap.MADV_FREE_REUSABLE" title="Link to this definition">¶</a></dt>
  520. <dt class="sig sig-object py" id="mmap.MADV_FREE_REUSE">
  521. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MADV_FREE_REUSE</span></span><a class="headerlink" href="#mmap.MADV_FREE_REUSE" title="Link to this definition">¶</a></dt>
  522. <dd><p>These options can be passed to <a class="reference internal" href="#mmap.mmap.madvise" title="mmap.mmap.madvise"><code class="xref py py-meth docutils literal notranslate"><span class="pre">mmap.madvise()</span></code></a>. Not every option will
  523. be present on every system.</p>
  524. <p>Availability: Systems with the madvise() system call.</p>
  525. <div class="versionadded">
  526. <p><span class="versionmodified added">New in version 3.8.</span></p>
  527. </div>
  528. </dd></dl>
  529. </section>
  530. <section id="map-constants">
  531. <span id="id1"></span><h2>MAP_* Constants<a class="headerlink" href="#map-constants" title="Link to this heading">¶</a></h2>
  532. <dl class="py data">
  533. <dt class="sig sig-object py" id="mmap.MAP_SHARED">
  534. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MAP_SHARED</span></span><a class="headerlink" href="#mmap.MAP_SHARED" title="Link to this definition">¶</a></dt>
  535. <dt class="sig sig-object py" id="mmap.MAP_PRIVATE">
  536. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MAP_PRIVATE</span></span><a class="headerlink" href="#mmap.MAP_PRIVATE" title="Link to this definition">¶</a></dt>
  537. <dt class="sig sig-object py" id="mmap.MAP_DENYWRITE">
  538. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MAP_DENYWRITE</span></span><a class="headerlink" href="#mmap.MAP_DENYWRITE" title="Link to this definition">¶</a></dt>
  539. <dt class="sig sig-object py" id="mmap.MAP_EXECUTABLE">
  540. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MAP_EXECUTABLE</span></span><a class="headerlink" href="#mmap.MAP_EXECUTABLE" title="Link to this definition">¶</a></dt>
  541. <dt class="sig sig-object py" id="mmap.MAP_ANON">
  542. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MAP_ANON</span></span><a class="headerlink" href="#mmap.MAP_ANON" title="Link to this definition">¶</a></dt>
  543. <dt class="sig sig-object py" id="mmap.MAP_ANONYMOUS">
  544. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MAP_ANONYMOUS</span></span><a class="headerlink" href="#mmap.MAP_ANONYMOUS" title="Link to this definition">¶</a></dt>
  545. <dt class="sig sig-object py" id="mmap.MAP_POPULATE">
  546. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MAP_POPULATE</span></span><a class="headerlink" href="#mmap.MAP_POPULATE" title="Link to this definition">¶</a></dt>
  547. <dt class="sig sig-object py" id="mmap.MAP_STACK">
  548. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MAP_STACK</span></span><a class="headerlink" href="#mmap.MAP_STACK" title="Link to this definition">¶</a></dt>
  549. <dt class="sig sig-object py" id="mmap.MAP_ALIGNED_SUPER">
  550. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MAP_ALIGNED_SUPER</span></span><a class="headerlink" href="#mmap.MAP_ALIGNED_SUPER" title="Link to this definition">¶</a></dt>
  551. <dt class="sig sig-object py" id="mmap.MAP_CONCEAL">
  552. <span class="sig-prename descclassname"><span class="pre">mmap.</span></span><span class="sig-name descname"><span class="pre">MAP_CONCEAL</span></span><a class="headerlink" href="#mmap.MAP_CONCEAL" title="Link to this definition">¶</a></dt>
  553. <dd><p>These are the various flags that can be passed to <a class="reference internal" href="#mmap.mmap" title="mmap.mmap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">mmap.mmap()</span></code></a>. <a class="reference internal" href="#mmap.MAP_ALIGNED_SUPER" title="mmap.MAP_ALIGNED_SUPER"><code class="xref py py-data docutils literal notranslate"><span class="pre">MAP_ALIGNED_SUPER</span></code></a>
  554. is only available at FreeBSD and <a class="reference internal" href="#mmap.MAP_CONCEAL" title="mmap.MAP_CONCEAL"><code class="xref py py-data docutils literal notranslate"><span class="pre">MAP_CONCEAL</span></code></a> is only available at OpenBSD. Note
  555. that some options might not be present on some systems.</p>
  556. <div class="versionchanged">
  557. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added <a class="reference internal" href="#mmap.MAP_POPULATE" title="mmap.MAP_POPULATE"><code class="xref py py-data docutils literal notranslate"><span class="pre">MAP_POPULATE</span></code></a> constant.</p>
  558. </div>
  559. <div class="versionadded">
  560. <p><span class="versionmodified added">New in version 3.11: </span>Added <a class="reference internal" href="#mmap.MAP_STACK" title="mmap.MAP_STACK"><code class="xref py py-data docutils literal notranslate"><span class="pre">MAP_STACK</span></code></a> constant.</p>
  561. </div>
  562. <div class="versionadded">
  563. <p><span class="versionmodified added">New in version 3.12: </span>Added <a class="reference internal" href="#mmap.MAP_ALIGNED_SUPER" title="mmap.MAP_ALIGNED_SUPER"><code class="xref py py-data docutils literal notranslate"><span class="pre">MAP_ALIGNED_SUPER</span></code></a> constant.
  564. Added <a class="reference internal" href="#mmap.MAP_CONCEAL" title="mmap.MAP_CONCEAL"><code class="xref py py-data docutils literal notranslate"><span class="pre">MAP_CONCEAL</span></code></a> constant.</p>
  565. </div>
  566. </dd></dl>
  567. </section>
  568. </section>
  569. <div class="clearer"></div>
  570. </div>
  571. </div>
  572. </div>
  573. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  574. <div class="sphinxsidebarwrapper">
  575. <div>
  576. <h3><a href="../contents.html">Table of Contents</a></h3>
  577. <ul>
  578. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmap</span></code> — Memory-mapped file support</a><ul>
  579. <li><a class="reference internal" href="#madv-constants">MADV_* Constants</a></li>
  580. <li><a class="reference internal" href="#map-constants">MAP_* Constants</a></li>
  581. </ul>
  582. </li>
  583. </ul>
  584. </div>
  585. <div>
  586. <h4>Previous topic</h4>
  587. <p class="topless"><a href="signal.html"
  588. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code> — Set handlers for asynchronous events</a></p>
  589. </div>
  590. <div>
  591. <h4>Next topic</h4>
  592. <p class="topless"><a href="netdata.html"
  593. title="next chapter">Internet Data Handling</a></p>
  594. </div>
  595. <div role="note" aria-label="source link">
  596. <h3>This Page</h3>
  597. <ul class="this-page-menu">
  598. <li><a href="../bugs.html">Report a Bug</a></li>
  599. <li>
  600. <a href="https://github.com/python/cpython/blob/main/Doc/library/mmap.rst"
  601. rel="nofollow">Show Source
  602. </a>
  603. </li>
  604. </ul>
  605. </div>
  606. </div>
  607. <div id="sidebarbutton" title="Collapse sidebar">
  608. <span>«</span>
  609. </div>
  610. </div>
  611. <div class="clearer"></div>
  612. </div>
  613. <div class="related" role="navigation" aria-label="related navigation">
  614. <h3>Navigation</h3>
  615. <ul>
  616. <li class="right" style="margin-right: 10px">
  617. <a href="../genindex.html" title="General Index"
  618. >index</a></li>
  619. <li class="right" >
  620. <a href="../py-modindex.html" title="Python Module Index"
  621. >modules</a> |</li>
  622. <li class="right" >
  623. <a href="netdata.html" title="Internet Data Handling"
  624. >next</a> |</li>
  625. <li class="right" >
  626. <a href="signal.html" title="signal — Set handlers for asynchronous events"
  627. >previous</a> |</li>
  628. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  629. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  630. <li class="switchers">
  631. <div class="language_switcher_placeholder"></div>
  632. <div class="version_switcher_placeholder"></div>
  633. </li>
  634. <li>
  635. </li>
  636. <li id="cpython-language-and-version">
  637. <a href="../index.html">3.12.3 Documentation</a> &#187;
  638. </li>
  639. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  640. <li class="nav-item nav-item-2"><a href="ipc.html" >Networking and Interprocess Communication</a> &#187;</li>
  641. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmap</span></code> — Memory-mapped file support</a></li>
  642. <li class="right">
  643. <div class="inline-search" role="search">
  644. <form class="inline-search" action="../search.html" method="get">
  645. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  646. <input type="submit" value="Go" />
  647. </form>
  648. </div>
  649. |
  650. </li>
  651. <li class="right">
  652. <label class="theme-selector-label">
  653. Theme
  654. <select class="theme-selector" oninput="activateTheme(this.value)">
  655. <option value="auto" selected>Auto</option>
  656. <option value="light">Light</option>
  657. <option value="dark">Dark</option>
  658. </select>
  659. </label> |</li>
  660. </ul>
  661. </div>
  662. <div class="footer">
  663. &copy;
  664. <a href="../copyright.html">
  665. Copyright
  666. </a>
  667. 2001-2024, Python Software Foundation.
  668. <br />
  669. This page is licensed under the Python Software Foundation License Version 2.
  670. <br />
  671. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  672. <br />
  673. See <a href="/license.html">History and License</a> for more information.<br />
  674. <br />
  675. The Python Software Foundation is a non-profit corporation.
  676. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  677. <br />
  678. <br />
  679. Last updated on Apr 09, 2024 (13:47 UTC).
  680. <a href="/bugs.html">Found a bug</a>?
  681. <br />
  682. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  683. </div>
  684. </body>
  685. </html>
上海开阖软件有限公司 沪ICP备12045867号-1