Python子进程不返回

Maw*_*awg 3 python subprocess python-2.7

我想从 Jenkins 调用 Python 脚本并让它构建我的应用程序,将其通过 FTP 传输到目标并运行它。

我正在尝试构建,但subprocess命令失败。我已经尝试过使用 和subprocess.call(),subprocess.popen()得到相同的结果。

shellCommand当我从命令行评估并运行它时,构建成功。

请注意,我有 3 个 shell 命令:1)删除工作目录,2)创建一个新的空工作目录,然后 3)构建。前两个命令从 中返回subprocess,但第三个命令挂起(尽管它在从命令行运行时完成)。

我做错了什么?或者,我有什么选择来执行该命令?

# +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
def ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand):
    try:
        process = subprocess.call(shellCommand, shell=True, stdout=subprocess.PIPE)
        #process.wait()
        return process #.returncode

    except KeyboardInterrupt, e: # Ctrl-C
        raise e
    except SystemExit, e: # sys.exit()
        raise e
    except Exception, e:
        print 'Exception while executing shell command : ' + shellCommand
        print str(e)
        traceback.print_exc()
        os._exit(1)

# +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
def BuildApplciation(arguments):
    # See http://gnuarmeclipse.github.io/advanced/headless-builds/

    jenkinsWorkspaceDirectory = arguments.eclipseworkspace + '/jenkins'

    shellCommand = 'rm -r ' + jenkinsWorkspaceDirectory
    ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand)

    shellCommand = 'mkdir ' + jenkinsWorkspaceDirectory
    if not ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand) == 0:
        print "Error making Jenkins work directory in Eclipse workspace : " + jenkinsWorkspaceDirectory
        return False

    application = 'org.eclipse.cdt.managedbuilder.core.headlessbuild'
    shellCommand = 'eclipse -nosplash -application ' + application + ' -import ' + arguments.buildRoot + '/../Project/ -build myAppApp/TargetRelease -cleanBuild    myAppApp/TargetRelease -data ' + jenkinsWorkspaceDirectory + ' -D DO_APPTEST'
    if not ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand) == 0:
        print "Error in build"
        return False
Run Code Online (Sandbox Code Playgroud)

Maw*_*awg 6

我进一步谷歌搜索并找到了这个页面,其中 1.2 说

访问已执行命令的输出的一种方法是在参数 stdout 或 stderr 中使用 PIPE,但如果子进程生成足够的输出到管道以填充操作系统管道缓冲区,则子进程将阻塞,因为管道不是正在读取。

果然,当我, stdout=subprocess.PIPE从上面的代码中删除 时,它按预期工作。

由于我只想要子进程的退出代码,因此上面的代码对我来说已经足够了。如果您想要命令的输出,请阅读链接页面。