首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >subprocess.Popen线程不安全吗?

subprocess.Popen线程不安全吗?
EN

Stack Overflow用户
提问于 2014-01-17 19:36:22
回答 1查看 7K关注 0票数 12

下面的简单脚本间歇性地挂在subprocess.Popen调用上(大约30%的时间)。

除非use_lock = True,然后它永远不会挂起,使我相信子进程不是线程安全的!预期的行为是脚本在5-6秒内完成。

要演示这个bug,只需运行几次"python bugProof.py“,直到它挂起。Ctrl-C出口。你会看到“post-Popen”只出现一次或两次,而不是第三次。

代码语言:javascript
复制
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):

代码语言:javascript
复制
#!/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清华9月26日17:14:58美国东部时间2013年x86_64 GNU/Linux

Python 2.7.3 (默认情况,2013年1月22日,11:34:30)

GCC 4.4.6 20120305 (红帽4.4.6-4)在linux2上

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-08 22:33:19

在Python2.x上,有各种影响subprocess.Popen的竞争条件。(例如,在2.7上,它禁用和恢复垃圾收集以防止各种计时问题,但这本身并不是线程安全的)。有关这一领域的一些问题,请参见http://bugs.python.org/issue2320http://bugs.python.org/issue1336http://bugs.python.org/issue14548

在Python3.2中对子流程进行了实质性的修订,它解决了这些问题(除其他外,叉& exec代码在C模块中,而不是在叉和exec之间的关键部分执行一些涉及合理的Python代码),并且可以移植到subprocess32模块中最近的Python2.x版本中。请注意PyPI页面中的以下内容:“在POSIX系统中,当在线程应用程序中使用时,它保证是可靠的。”

我可以复制上面代码的偶尔崩溃(对我来说大约是25% ),但是在使用import subprocess32 as subprocess之后,我还没有看到100+运行中出现过任何故障。

请注意,subprocess32 (和Python 3.2+)默认为close_fds=True,但在subprocess32就绪后,即使使用close_fds=False也没有出现故障(并不是说您通常需要这样做)。

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21194380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档