admin管理员组文章数量:1201640
I have a long build script which is a mix of "real" python code and lengthy subprocess.run()
calls to things like debootstrap
, wget
, apt-get
or build.sh
.
Everything is parallelized. One thread does debootstrap
followed by apt-get
, while another does build.sh
, then both are joined and we start more threads for combining the results and so on. Each thread logs to a dedicated file.
While looking for a generic way to log the output of subprocess.run()
started by one of these threads, I came upon the following answer:
Is it a bug to call subprocess.run()
in a python script with more than one thread?
I have a long build script which is a mix of "real" python code and lengthy subprocess.run()
calls to things like debootstrap
, wget
, apt-get
or build.sh
.
Everything is parallelized. One thread does debootstrap
followed by apt-get
, while another does build.sh
, then both are joined and we start more threads for combining the results and so on. Each thread logs to a dedicated file.
While looking for a generic way to log the output of subprocess.run()
started by one of these threads, I came upon the following answer: https://stackoverflow.com/a/31868783/876832
Is it a bug to call subprocess.run()
in a python script with more than one thread?
1 Answer
Reset to default 5Is it a bug to call
subprocess.run()
in a python script with more than one thread?
No. I do that all the time.
I think the answer you linked is misguided. After all, you don't even know whether subprocess.Popen()
and its facades like subprocess.run()
use the fork
syscall (especially on Windows, they certainly don't, since that call doesn't exist there).
Sure, if you were to manually os.fork()
, I'd exercise caution in a threaded program (and indeed, as the docs say, since 3.12, if Python detects you're doing that, it'll warn you).
Under the hood, though, on POSIX systems, subprocess.Popen()
uses os.posix_spawn
when possible, and otherwise a very careful fork_exec
implementation.
Oh, and as an aside, the question you linked actually uses the Python 2.7-era low-level thread
module, these days aptly called _thread
, the low-level threading API. That's a different low-level beast than threading.Thread
s.
本文标签: pythonIs it a bug to use subprocessrun() in a multithreaded scriptStack Overflow
版权声明:本文标题:python - Is it a bug to use subprocess.run() in a multithreaded script? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738564780a2099932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论