- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
以下简单脚本间歇性地卡在 subprocess.Popen 调用上(大约 30% 的时间)。
除非 use_lock = True,否则它永远不会挂起,这让我相信子进程不是线程安全的!预期行为是脚本在 5-6 秒内完成。
要演示错误,只需运行“python bugProof.py”几次直到它挂起。 Ctrl-C 退出。您会看到“post-Popen”仅出现一两次,但不会出现第三次。
import subprocess, threading, fcntl, os, time
end_time = time.time()+5
lock = threading.Lock()
use_lock = False
path_to_factorial = os.path.join(os.path.dirname(os.path.realpath(__file__)),'factorial.sh')
def testFunction():
print threading.current_thread().name, '| pre-Popen'
if use_lock: lock.acquire()
p = subprocess.Popen([path_to_factorial], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if use_lock: lock.release()
print threading.current_thread().name, '| post-Popen'
fcntl.fcntl(p.stdout, fcntl.F_SETFL, os.O_NONBLOCK)
fcntl.fcntl(p.stderr, fcntl.F_SETFL, os.O_NONBLOCK)
while time.time()<end_time:
try: p.stdout.read()
except: pass
try: p.stderr.read()
except: pass
print threading.current_thread().name, '| DONE'
for i in range(3):
threading.Thread(target=testFunction).start()
上面引用的 shell 脚本 (factorial.sh):
#!/bin/sh
echo "Calculating factorial (anything that's somewhat compute intensive, this script takes 3 sec on my machine"
ans=1
counter=0
fact=999
while [ $fact -ne $counter ]
do
counter=`expr $counter + 1`
ans=`expr $ans \* $counter`
done
echo "Factorial calculation done"
read -p "Test input (this part is critical for bug to occur): " buf
echo "$buf"
系统信息:Linux 2.6.32-358.123.2.openstack.el6.x86_64 #1 SMP Thu Sep 26 17:14:58 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux
Python 2.7.3(默认,2013 年 1 月 22 日,11:34:30)
linux2 上的 [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)]
最佳答案
在 Python 2.x 上,存在影响 subprocess.Popen 的各种竞争条件。 (例如,在 2.7 上它禁用并恢复垃圾收集以防止各种计时问题,但这本身不是线程安全的)。参见例如http://bugs.python.org/issue2320 , http://bugs.python.org/issue1336和 http://bugs.python.org/issue14548对于这方面的一些问题。
在 Python 3.2 中对子进程进行了实质性修改,解决了这些问题(除其他事项外,fork 和 exec 代码位于 C 模块中,而不是在 fork 和 exec 之间的关键部分做一些合理涉及的 Python 代码),并且可以反向移植到 subprocess32 中最近的 Python 2.x 版本模块。请注意 PyPI 页面中的以下内容:“在 POSIX 系统上,保证在线程应用程序中使用时是可靠的。”
我可以重现上面代码的偶尔崩溃(对我来说大约是 25%),但是在使用 import subprocess32 as subprocess
之后,我在 100 多次运行中没有看到任何失败。
请注意,subprocess32(和 Python 3.2+)默认为 close_fds=True,但是在 subprocess32 到位的情况下,即使使用 close_fds=False,我也没有看到任何失败(并不是说您通常应该需要它)。
关于python - subprocess.Popen 不是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21194380/
我在这里看到这个帖子很多次了;但未能从命令中捕获故意错误。迄今为止我找到的最好的部分工作.. from Tkinter import * import os import Image, ImageTk
我正在尝试使用 Python 在我的服务器上进行一些基本的模块设置。这有点困难,因为我无法访问互联网。 这是我的代码 import sys import os from subprocess impo
这个问题在这里已经有了答案: Why does passing variables to subprocess.Popen not work despite passing a list of ar
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
使用 subprocess.call 时,输出符合预期。 result = subprocess.call([securefx, '/NoPrompt', '/Q', '/RetryCount', r
当我使用 subprocess.Popen 或 subprocess.call 执行生成大量输出的命令行时,python 脚本挂起,但奇怪的是,等待一段时间后,脚本处于挂起状态,发现cmd命令的作业已
我是 subprocess 模块的新手,文档让我想知道 subprocess.popen 和 subprocess.run 之间有什么区别.命令的作用有区别吗?一个只是更新吗?哪个更好用? 最佳答案
我需要限制使用 subprocess.call 从 python 进程生成的外部命令行应用程序占用的时间和 CPU,主要是因为有时生成的进程会卡住并将 CPU 固定在 99%。 nice 和 ulim
我一直在使用 subprocess.check_output()有一段时间从子进程捕获输出,但在某些情况下遇到了一些性能问题。我在 RHEL6 机器上运行它。 调用 Python 环境是 linux
我想从 python 运行一个程序并找到它的内存使用情况。为此,我正在使用: l=['./a.out','','out.txt'] p=subprocess.Popen(l,shell=False,s
我正在使用 Python 2.7 我正在尝试从 Python 运行 StatTransfer 程序。 当我尝试时: tempname = os.path.abspath('./text.txt') T
我想执行以下操作: 使用 subprocess.check_call 从 Python 外壳到另一个可执行文件 捕获子进程的 stderr(如果有) 将 stderr 输出添加到父进程的 Called
我编写了一个程序(myProg.py),它使用subprocess模块通过run函数运行其他python程序。我注意到这些其他 python 程序中的 input(arg) 语句中的 arg 没有
我有这个小脚本可以让您的无线设备进入监控模式。它执行 airodump 扫描,然后在终止扫描后将输出转储到 file.txt 或变量,这样我就可以抓取 BSSID 和我可能需要的任何其他信息。 我觉得
我最近在 Python 中注意到 subprocess.Popen() 有一个参数: stdout=None(default) 我还看到有人使用 stdout=subprocess.PIPE。 有什么
我已经查看了它们的文档。 这个问题是由 J.F. 在这里的评论提示的:Retrieving the output of subprocess.call() subprocess.call() 的当前
我一直在尝试了解 subprocess.call 和 subprocess.run 之间的区别。我知道最后一个是 Python 3.5 上的新版本,两者都基于 subprocess.Popen,但我还
我无法得到它与 bash 相关或 python 子进程,但结果不同: >>> subprocess.Popen("echo $HOME", shell=True, stdout=subprocess.
我正在编写一个需要在 Linux 和 Windows 上运行并使用路径中存在的可执行文件(带参数)的程序。 (假设) 目前,我在使用 Subprocess.Call 和 Subprocess.Pope
当我的脚本有 .pyw扩展,函数 subprocess.Popen不起作用,但如果我使用 .py扩展,它的工作原理。其实扩展并不是那么重要,关键是我是否使用终端来运行脚本,如果我不使用它我有问题,否则
我是一名优秀的程序员,十分优秀!