gpt4 book ai didi

python - subprocess.Popen 不是线程安全的吗?

转载 作者:太空狗 更新时间:2023-10-29 18:03:05 25 4
gpt4 key购买 nike

以下简单脚本间歇性地卡在 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/issue1336http://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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com