{"id":329490,"date":"2023-09-25T14:00:00","date_gmt":"2023-09-25T14:00:00","guid":{"rendered":"https:\/\/realpython.com\/python312-subinterpreters\/"},"modified":"2023-09-25T14:00:00","modified_gmt":"2023-09-25T14:00:00","slug":"python-3-12-preview-subinterpreters","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/09\/25\/python-3-12-preview-subinterpreters\/","title":{"rendered":"Python 3.12 Preview: Subinterpreters"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<div>\n<p>With the upcoming release of Python 3.12 this fall and Python 3.13 following a year later, you might have heard about how <strong>Python subinterpreters<\/strong> are changing. The upcoming changes will first give extension module developers better control of the GIL and parallelism, potentially speeding up your programs. <\/p>\n<p>The following release may take this even further and allow you to use subinterpreters from Python directly, making it easier for you to incorporate them into your programs!<\/p>\n<p><strong>In this tutorial, you\u2019ll:<\/strong><\/p>\n<ul>\n<li>Get a high-level view of what <strong>Python subinterpreters<\/strong> are<\/li>\n<li>Learn how changes to <strong>CPython\u2019s global state<\/strong> in Python 3.12 may change things for you<\/li>\n<li>Get a glimpse of what changes might be coming for subinterpreters in <strong>Python 3.13<\/strong><\/li>\n<\/ul>\n<p>To get the most out of this tutorial, you should be familiar with the <a href=\"https:\/\/realpython.com\/learning-paths\/python-basics\/\">basics of Python<\/a>, as well as with <a href=\"https:\/\/realpython.com\/python-gil\/\">the global interpreter lock (GIL)<\/a> and <a href=\"https:\/\/realpython.com\/python-concurrency\/\">concurrency<\/a>. You\u2019ll encounter some <a href=\"https:\/\/realpython.com\/c-for-python-programmers\/\">C<\/a> code, but only a little.<\/p>\n<p>You\u2019ll find many other new features, improvements, and optimizations in Python 3.12. The most relevant ones include the following:<\/p>\n<ul>\n<li><a href=\"https:\/\/realpython.com\/python312-error-messages\/\">Ever better error messages<\/a><\/li>\n<li><a href=\"https:\/\/realpython.com\/python312-perf-profiler\/\">Support for the Linux <code>perf<\/code> profiler<\/a><\/li>\n<li><a href=\"https:\/\/realpython.com\/python312-f-strings\/\">More powerful f-strings<\/a><\/li>\n<li><a href=\"https:\/\/realpython.com\/python312-typing\/\">Improved static typing features<\/a><\/li>\n<\/ul>\n<p>Go ahead and check out <a href=\"https:\/\/docs.python.org\/3.12\/whatsnew\/3.12.html\">what\u2019s new<\/a> in the changelog for more details on these and other features. It\u2019s definitely worth your time to explore what\u2019s coming!<\/p>\n<div class=\"alert alert-warning\" role=\"alert\">\n<p><strong markdown=\"1\">Free Bonus:<\/strong> <a href=\"https:\/\/realpython.com\/bonus\/python-312-code\/\" class=\"alert-link\" data-toggle=\"modal\" data- data-focus=\"false\" markdown=\"1\">Click here to download your sample code<\/a> for a sneak peek at Python 3.12, coming in October 2023.<\/p>\n<\/div>\n<h2 id=\"what-are-python-subinterpreters\">What Are Python Subinterpreters?<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python312-subinterpreters\/#what-are-python-subinterpreters\" title=\"Permanent link\"><\/a><\/h2>\n<p>Before you start thinking about subinterpreters, recall that an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Interpreter_(computing)\">interpreter<\/a> is a program that executes a script directly in a high-level language instead of translating it to machine code. In the case of most Python users, <a href=\"https:\/\/realpython.com\/cpython-source-code-guide\/\">CPython<\/a> is the interpreter you\u2019re running. A subinterpreter is a copy of the full CPython interpreter that can run independently from the <em>main<\/em> interpreter that started alongside your program.<\/p>\n<div class=\"alert alert-primary\" role=\"alert\">\n<p><strong>Note:<\/strong> The terms <em>interpreter<\/em> and <em>subinterpreter<\/em> get mixed together fairly commonly. For the purposes of this tutorial, you can view the <strong>main<\/strong> interpreter as the one that runs when your program starts. All other interpreters that start after that point are considered subinterpreters. Other than a few minor details, subinterpreters are the same type of object as the main interpreter.<\/p>\n<\/div>\n<p>Most of the state of the subinterpreter is separate from the main interpreter. This includes elements like the <a href=\"https:\/\/realpython.com\/python-use-global-variable-in-function\/#the-globals-function\">global scope name table<\/a> and the modules that get imported. However, this doesn\u2019t include some of the items that the operating system provides to the process, like <a href=\"https:\/\/realpython.com\/why-close-file-python\/\">file handles<\/a> and memory.<\/p>\n<p>This is different from <a href=\"https:\/\/realpython.com\/intro-to-python-threading\/\">threading<\/a> or other forms of <a href=\"https:\/\/realpython.com\/python-parallel-processing\/\">concurrency<\/a> in that threads can share the same context and global state while allowing a separate flow of execution. For example, if you start a new thread, then it still has the same global scope name table.<\/p>\n<p>A subinterpreter, however, can be described as a <a href=\"https:\/\/docs.python.org\/3\/c-api\/init.html#c.PyInterpreterState\">collection of cooperating threads<\/a> that have some shared state. These threads will have the same set of imports, independent of other subinterpreters. Spawning new threads in a subinterpreter adds new threads to this collection, which won\u2019t be visible from other interpreters.<\/p>\n<p>Some of the upcoming changes that you\u2019ll see will also allow subinterpreters to improve <a href=\"https:\/\/en.wikipedia.org\/wiki\/Parallel_computing\">parallelism<\/a> in Python programs.<\/p>\n<p>Subinterpreters have been a part of the Python language <a href=\"https:\/\/www.python.org\/download\/releases\/1.5\/whatsnew\/\">since version 1.5<\/a>, but they\u2019ve only been available as part of the C-API, not from Python. But there are large changes coming that will make them more useful and interesting for everyday Python users.<\/p>\n<h2 id=\"whats-changing-in-python-312-pep-684\">What\u2019s Changing in Python 3.12 (PEP 684)?<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python312-subinterpreters\/#whats-changing-in-python-312-pep-684\" title=\"Permanent link\"><\/a><\/h2>\n<p>Now that you know what a Python subinterpreter is, you\u2019ll take a look at what\u2019s changing in the upcoming releases of CPython.<\/p>\n<p>Most of the subinterpreter changes are described in two proposals, <strong>PEP 684<\/strong> and <strong>PEP 554<\/strong>. Only PEP 684 will make it into the 3.12 release. PEP 554 is scheduled for the 3.13 release but hasn\u2019t been officially approved yet.<\/p>\n<h3 id=\"changes-to-the-global-state-and-the-gil\">Changes to the Global State and the GIL<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python312-subinterpreters\/#changes-to-the-global-state-and-the-gil\" title=\"Permanent link\"><\/a><\/h3>\n<p>The main focus of <a href=\"https:\/\/peps.python.org\/pep-0684\/\">PEP 684<\/a> is refactoring the internals of the CPython source code so that each subinterpreter can have its own <a href=\"https:\/\/realpython.com\/python-gil\/\">global interpreter lock (GIL)<\/a>. The GIL is a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Lock_(computer_science)\">lock<\/a>, or mutex, which allows only one thread to have control of the Python interpreter. Until this PEP, there was a single GIL for all subinterpreters, which meant that no matter how many subinterpreters you created, only one could run at a single time.<\/p>\n<p>Moving the GIL so that each subinterpreter has a separate lock is a great idea. So, why hasn\u2019t it been done already? The issue is that the GIL is preventing multiple threads from accessing some of the global state of CPython simultaneously, so it\u2019s protecting your program from bugs that race conditions could cause.<\/p>\n<p>The core developers needed to move much of the previously global state into per-interpreter storage, meaning each interpreter has its own independent version:<\/p>\n<figure class=\"js-lightbox\"><a href=\"https:\/\/files.realpython.com\/media\/interpreter-storage.3b268b1beba9.png\" ><img decoding=\"async\" loading=\"lazy\" class=\"img-fluid mx-auto d-block \" src=\"https:\/\/files.realpython.com\/media\/interpreter-storage.3b268b1beba9.png\" width=\"1413\" height=\"723\" srcset=\"https:\/\/robocrop.realpython.net\/?url=https%3A\/\/files.realpython.com\/media\/interpreter-storage.3b268b1beba9.png&amp;w=353&amp;sig=a9a14ea3e642206281700a513d7a331202962f20 353w, https:\/\/robocrop.realpython.net\/?url=https%3A\/\/files.realpython.com\/media\/interpreter-storage.3b268b1beba9.png&amp;w=471&amp;sig=d474eeed13aea40e13402d9bb3ce62d0bee4744e 471w, https:\/\/robocrop.realpython.net\/?url=https%3A\/\/files.realpython.com\/media\/interpreter-storage.3b268b1beba9.png&amp;w=706&amp;sig=783f2d5c47c0a0baa30629a4b86e585a44abddb9 706w, https:\/\/files.realpython.com\/media\/interpreter-storage.3b268b1beba9.png 1413w\" sizes=\"(min-width: 1200px) 690px, (min-width: 780px) calc(-5vw + 669px), (min-width: 580px) 510px, calc(100vw - 30px)\" alt=\"Diagram showing global state surrounds two interpreters, each with it's own per-interpreter state and set of threads.\" data-asset=\"5388\"><\/a><\/figure>\n<\/div>\n<h2><a href=\"https:\/\/realpython.com\/python312-subinterpreters\/?utm_source=realpython&#038;utm_medium=rss\">Read the full article at https:\/\/realpython.com\/python312-subinterpreters\/ \u00bb<\/a><\/h2>\n<hr \/>\n<p><em>[ Improve Your Python With ? Python Tricks ? \u2013 Get a short &amp; sweet Python Trick delivered to your inbox every couple of days. <a href=\"https:\/\/realpython.com\/python-tricks\/?utm_source=realpython&amp;utm_medium=rss&amp;utm_campaign=footer\">&gt;&gt; Click here to learn more and see examples<\/a> ]<\/em><\/p>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this tutorial, you&#8217;ll preview one of the upcoming features of Python 3.12 and a proposed change to Python 3.13, addressing how subinterpreters work in the CPython program. The changes are described in PEP 684 and PEP 554.<\/p>\n<\/div>","protected":false},"author":2055,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Python 3.12 Preview: Subinterpreters - ITTeacherITFreelance.hk","description":"In this tutorial, you'll preview one of the upcoming features of Python 3.12 and a proposed change to Python 3.13, addressing how subinterpreters work in the CP"},"footnotes":""},"categories":[10700],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329490"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2055"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329490"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329490\/revisions"}],"predecessor-version":[{"id":329491,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329490\/revisions\/329491"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}