Python子进程未返回

2024-10-01 13:32:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从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

Tags: 命令行命令returnapplicationshellcallprocessarguments
1条回答
网友
1楼 · 发布于 2024-10-01 13:32:09

我在google上进一步搜索发现了this page,在1.2处显示

One way of gaining access to the output of the executed command would be to use PIPE in the arguments stdout or stderr, but the child process will block if it generates enough output to a pipe to fill up the OS pipe buffer as the pipes are not being read from.

果然,当我从上面的代码中删除, stdout=subprocess.PIPE时,它就如预期的那样工作了。在

由于我只需要子进程的退出代码,以上代码对我来说就足够了。如果需要命令的输出,请阅读链接页。在

相关问题 更多 >