- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图捕获 check_output 的返回值,而不是让它自动打印到命令行。不幸的是,我的解决方案不起作用,我不知道为什么。我已经包含了我的代码及其输出:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from multiprocessing import Pool
from subprocess import check_output,CalledProcessError
def job(cmd):
result = ""
try:
result = check_output(cmd.split()) # Split string into list.
print("job result length = {0}".format(len(result)), file=sys.stdout)
except CalledProcessError as error:
raise Exception("Exit status of the child process: {0}\
Command used to spawn child process: {1}\
Output of the child process: {2}".format(error.returncode,error.cmd,error.output))
def main():
# Sets up a process pool. Defaults to number of cores.
# Each input gets passed to job and processed in a separate process.
p = Pool()
result = []
try:
# cmd_list is just a list of system commands which have been verified to work.
result = list(p.imap_unordered(job, cmd_list))
print("main result length = {0}".format(len(result)), file=sys.stdout)
print("{0}".format(result), file=sys.stdout)
except Exception as error:
print("Error: {0}. Aborting...".format(error), file=sys.stderr)
p.close()
p.terminate()
else:
p.close()
p.join()
if __name__ == '__main__':
main()
输出
除了 check_output 执行的每个命令的输出之外,我的打印语句还显示了一些意想不到的结果:
job result length = 0
job result length = 0
main result length = 2
[None, None]
我希望作业结果长度等于 2,并且结果包含子进程的返回值。
最佳答案
result
是一个局部变量。要么返回它:
def job(cmd):
# something goes here
return result
或者使其全局化:
result = ""
def job(cmd):
global result
# something goes here
result = whatever it shall be.
或者参数化它:
def job(cmd, result):
result = whatever it shall be.
关于python - 捕获 check_output 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30516918/
我正在玩子流程库。我可以执行 v = subprocess.check_output("ls") 而且效果很好。但是,当我通过 SSH 连接到一台机器上并想要执行 hadoop 命令时, v = su
我有使用 subprocess.check_output 的代码很好,我不得不重新安装 Ubuntu 16.04,然后它提示它现在找不到属性 check_output。 import subproce
我正在尝试为我正在编写的 configure.py 脚本检索 CPU 功能列表。在 shell 中,我执行如下操作: $ cat /proc/cpuinfo|grep flags|head -1|cu
#!/bin/python import subprocess import traceback import logging output_file = "/Users/abhabhin/Deskt
我在此处执行此代码时遇到问题 subprocess.check_output(['ps -ef | grep ftp | wc -l'],env=environ,shell=True) 当我从终端执行
首先,我阅读了尽可能多的与 subprocess.check_output() 相关的问题,但仍然很难找出问题所在。 如果我在 shell 中执行 kill -l 1,我会得到 1 对应的信号名称,即
我正在使用python来运行Youtube-dl ,一款用于从 YouTube 和其他网站下载视频的软件。我专门用它从 YouTube 视频下载音频。我可以通过使用 check_output() 执行
我正在使用 subprocess.check_output 运行 Xyce(SPICE 模拟器)仿真,因为我想使用它的结果进行进一步分析。 这是我使用的代码: x=subprocess.check_o
我正在尝试在 Python 2.7.5 中运行以下代码: output = subprocess.check_output(commandList) print (len(output)) 我的命令列
我正在通过 subprocess.check_output() 执行命令,因为我希望它的 o/p 存储在缓冲区中。 现在,在执行此操作时,如果命令失败或出现任何错误,则会给我的整个应用程序带来问题。
当我在 Subprocess 模块中使用 check_output 时,我从 Python shell 中得到了这个输出错误: AttributeError: 'module' object has
我正在尝试使用 mediainfo 读取视频文件的持续时间。这个 shell 命令有效 mediainfo --Inform="Video;%Duration/String3%" file 并产生类似
我在使用 subprocess.check_output() 时遇到了一些奇怪的问题。起初我只是使用 subprocess.call() 并且一切正常。然而,当我简单地将 call() 切换为 che
我想调用一个脚本,将字符串的内容通过管道传输到它的标准输入并检索它的标准输出。 我不想接触真正的文件系统,所以我不能为它创建真正的临时文件。 使用 subprocess.check_output 我可
prules=subprocess.check_output("perl ruleFinder.pl "+datasetfile+" "+myrulepath, shell=True)##call a
用crontab执行python脚本时,脚本缺少环境变量。 该脚本调用一个可执行文件,某些 Linux 发行版在 /usr/bin 下提供,而其他在 /sbin 下提供。 subprocess.che
我在我的 Raspberry Pi 3 上运行下面的代码行,并期望向用户显示输出。 (这只是我学习曲线中的一个测试) #test.py import subprocess output = subpr
我正在尝试获取的输出 dmesg | grep -i 'bios' 我已经试过了: dmsg = subprocess.check_output("dmesg | grep -i 'bios'").d
我正在使用 subprocess.check_output 调用 MS DOS 命令行应用程序以从应用程序返回 stdout 字符串。 check_ouput 的返回是关于刚刚运行的命令的信息,一个新
我试图捕获 check_output 的返回值,而不是让它自动打印到命令行。不幸的是,我的解决方案不起作用,我不知道为什么。我已经包含了我的代码及其输出: #!/usr/bin/env python3
我是一名优秀的程序员,十分优秀!