<legend id='tq5jA'><style id='tq5jA'><dir id='tq5jA'><q id='tq5jA'></q></dir></style></legend>

    <i id='tq5jA'><tr id='tq5jA'><dt id='tq5jA'><q id='tq5jA'><span id='tq5jA'><b id='tq5jA'><form id='tq5jA'><ins id='tq5jA'></ins><ul id='tq5jA'></ul><sub id='tq5jA'></sub></form><legend id='tq5jA'></legend><bdo id='tq5jA'><pre id='tq5jA'><center id='tq5jA'></center></pre></bdo></b><th id='tq5jA'></th></span></q></dt></tr></i><div id='tq5jA'><tfoot id='tq5jA'></tfoot><dl id='tq5jA'><fieldset id='tq5jA'></fieldset></dl></div>
      <bdo id='tq5jA'></bdo><ul id='tq5jA'></ul>
  • <tfoot id='tq5jA'></tfoot>

    <small id='tq5jA'></small><noframes id='tq5jA'>

        使用 subprocess 模块是否会释放 python GIL?

        时间:2023-07-21

          <small id='eg6pk'></small><noframes id='eg6pk'>

          <i id='eg6pk'><tr id='eg6pk'><dt id='eg6pk'><q id='eg6pk'><span id='eg6pk'><b id='eg6pk'><form id='eg6pk'><ins id='eg6pk'></ins><ul id='eg6pk'></ul><sub id='eg6pk'></sub></form><legend id='eg6pk'></legend><bdo id='eg6pk'><pre id='eg6pk'><center id='eg6pk'></center></pre></bdo></b><th id='eg6pk'></th></span></q></dt></tr></i><div id='eg6pk'><tfoot id='eg6pk'></tfoot><dl id='eg6pk'><fieldset id='eg6pk'></fieldset></dl></div>
          • <bdo id='eg6pk'></bdo><ul id='eg6pk'></ul>
          • <legend id='eg6pk'><style id='eg6pk'><dir id='eg6pk'><q id='eg6pk'></q></dir></style></legend>
              <tbody id='eg6pk'></tbody>

                  <tfoot id='eg6pk'></tfoot>
                1. 本文介绍了使用 subprocess 模块是否会释放 python GIL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当通过Python的subprocess模块调用一个耗时较长的linux二进制文件时,是否会释放GIL?

                  When calling a linux binary which takes a relatively long time through Python's subprocess module, does this release the GIL?

                  我想并行化一些从命令行调用二进制程序的代码.使用线程(通过 threadingmultiprocessing.pool.ThreadPool)还是 multiprocessing 更好?我的假设是,如果 subprocess 释放 GIL,那么选择 threading 选项会更好.

                  I want to parallelise some code which calls a binary program from the command line. Is it better to use threads (through threading and a multiprocessing.pool.ThreadPool) or multiprocessing? My assumption is that if subprocess releases the GIL then choosing the threading option is better.

                  推荐答案

                  当通过Python的subprocess模块调用一个耗时较长的linux二进制文件时,是否会释放GIL?

                  When calling a linux binary which takes a relatively long time through Python's subprocess module, does this release the GIL?

                  是的,它在调用过程中释放了全局解释器锁(GIL).

                  Yes, it releases the Global Interpreter Lock (GIL) in the calling process.

                  您可能知道,在 POSIX 平台上,subprocess 在来自 forkexecveexecve 的原始"组件之上提供了便利的接口code>waitpid.

                  As you are likely aware, on POSIX platforms subprocess offers convenience interfaces atop the "raw" components from fork, execve, and waitpid.

                  通过检查 CPython 2.7.9 源代码,forkexecve not 发布 GIL.但是,这些调用不会阻塞,所以我们不希望 GIL 被释放.

                  By inspection of the CPython 2.7.9 sources, fork and execve do not release the GIL. However, those calls do not block, so we'd not expect the GIL to be released.

                  waitpid 当然 确实 阻塞,但我们看到它的实现确实放弃了使用 ALLOW_THREADS 宏的 GIL:

                  waitpid of course does block, but we see it's implementation does give up the GIL using the ALLOW_THREADS macros:

                  static PyObject *
                  posix_waitpid(PyObject *self, PyObject *args)
                  {
                  ....
                  Py_BEGIN_ALLOW_THREADS
                  pid = waitpid(pid, &status, options);
                  Py_END_ALLOW_THREADS
                  ....
                  

                  这也可以通过调用一些长时间运行的程序来测试,例如 sleep 来自一个演示多线程 python 脚本.

                  This could also be tested by calling out to some long running program like sleep from a demonstration multithreaded python script.

                  这篇关于使用 subprocess 模块是否会释放 python GIL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python子进程readlines()? 下一篇:读取/写入 Popen() 子进程

                  相关文章

                  <i id='ypux0'><tr id='ypux0'><dt id='ypux0'><q id='ypux0'><span id='ypux0'><b id='ypux0'><form id='ypux0'><ins id='ypux0'></ins><ul id='ypux0'></ul><sub id='ypux0'></sub></form><legend id='ypux0'></legend><bdo id='ypux0'><pre id='ypux0'><center id='ypux0'></center></pre></bdo></b><th id='ypux0'></th></span></q></dt></tr></i><div id='ypux0'><tfoot id='ypux0'></tfoot><dl id='ypux0'><fieldset id='ypux0'></fieldset></dl></div>

                  1. <tfoot id='ypux0'></tfoot>

                        <bdo id='ypux0'></bdo><ul id='ypux0'></ul>

                    1. <legend id='ypux0'><style id='ypux0'><dir id='ypux0'><q id='ypux0'></q></dir></style></legend>

                      <small id='ypux0'></small><noframes id='ypux0'>