- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 celery 任务,它使用 subprocess
调用 Django 应用程序外部的其他 python 脚本。该程序中有一些 print
,我想将这些打印在我的 celery 日志文件或数据库中。当我在 Django settings.py
文件中设置 CELERY_ALWAYS_EAGER = True
时,一切正常。如果我不设置此设置,则 celery 任务仅在退出时记录子进程 stdout。似乎 p.stdout.readline()
正在阻塞。
run-test.py
是一个很长的过程,只有几分钟,但它会打印出它正在做什么。我想捕捉这个。
@shared_task
def run_tests(scenario_path, vu):
basedir = os.path.abspath(os.path.dirname(__file__))
config_path = '%s/../../scripts/config.ini' % basedir
cmd = ['python', '%s/../../scripts/aws/run-test.py' % basedir, '%s' % config_path, scenario_path, str(vu), str(2)]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
while True:
line = p.stdout.readline()
if line != '':
logger.info(line)
else:
return
最佳答案
我发现这非常有用,使用 select 进行轮询而不是在 readline 上阻塞。
https://gist.github.com/bgreenlee/1402841
child = subprocess.Popen(popenargs, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, **kwargs)
log_level = {child.stdout: stdout_log_level,
child.stderr: stderr_log_level}
def check_io():
ready_to_read = select.select([child.stdout, child.stderr], [], [], 1000)[0]
for io in ready_to_read:
line = io.readline()
logger.log(log_level[io], line[:-1])
# keep checking stdout/stderr until the child exits
while child.poll() is None:
check_io()
check_io() # check again to catch anything after the process exits
关于subprocess - Celery 任务子进程 stdout 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21559572/
我在这里看到这个帖子很多次了;但未能从命令中捕获故意错误。迄今为止我找到的最好的部分工作.. from Tkinter import * import os import Image, ImageTk
我正在尝试使用 Python 在我的服务器上进行一些基本的模块设置。这有点困难,因为我无法访问互联网。 这是我的代码 import sys import os from subprocess impo
这个问题在这里已经有了答案: Why does passing variables to subprocess.Popen not work despite passing a list of ar
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
使用 subprocess.call 时,输出符合预期。 result = subprocess.call([securefx, '/NoPrompt', '/Q', '/RetryCount', r
当我使用 subprocess.Popen 或 subprocess.call 执行生成大量输出的命令行时,python 脚本挂起,但奇怪的是,等待一段时间后,脚本处于挂起状态,发现cmd命令的作业已
我是 subprocess 模块的新手,文档让我想知道 subprocess.popen 和 subprocess.run 之间有什么区别.命令的作用有区别吗?一个只是更新吗?哪个更好用? 最佳答案
我需要限制使用 subprocess.call 从 python 进程生成的外部命令行应用程序占用的时间和 CPU,主要是因为有时生成的进程会卡住并将 CPU 固定在 99%。 nice 和 ulim
我一直在使用 subprocess.check_output()有一段时间从子进程捕获输出,但在某些情况下遇到了一些性能问题。我在 RHEL6 机器上运行它。 调用 Python 环境是 linux
我想从 python 运行一个程序并找到它的内存使用情况。为此,我正在使用: l=['./a.out','','out.txt'] p=subprocess.Popen(l,shell=False,s
我正在使用 Python 2.7 我正在尝试从 Python 运行 StatTransfer 程序。 当我尝试时: tempname = os.path.abspath('./text.txt') T
我想执行以下操作: 使用 subprocess.check_call 从 Python 外壳到另一个可执行文件 捕获子进程的 stderr(如果有) 将 stderr 输出添加到父进程的 Called
我编写了一个程序(myProg.py),它使用subprocess模块通过run函数运行其他python程序。我注意到这些其他 python 程序中的 input(arg) 语句中的 arg 没有
我有这个小脚本可以让您的无线设备进入监控模式。它执行 airodump 扫描,然后在终止扫描后将输出转储到 file.txt 或变量,这样我就可以抓取 BSSID 和我可能需要的任何其他信息。 我觉得
我最近在 Python 中注意到 subprocess.Popen() 有一个参数: stdout=None(default) 我还看到有人使用 stdout=subprocess.PIPE。 有什么
我已经查看了它们的文档。 这个问题是由 J.F. 在这里的评论提示的:Retrieving the output of subprocess.call() subprocess.call() 的当前
我一直在尝试了解 subprocess.call 和 subprocess.run 之间的区别。我知道最后一个是 Python 3.5 上的新版本,两者都基于 subprocess.Popen,但我还
我无法得到它与 bash 相关或 python 子进程,但结果不同: >>> subprocess.Popen("echo $HOME", shell=True, stdout=subprocess.
我正在编写一个需要在 Linux 和 Windows 上运行并使用路径中存在的可执行文件(带参数)的程序。 (假设) 目前,我在使用 Subprocess.Call 和 Subprocess.Pope
当我的脚本有 .pyw扩展,函数 subprocess.Popen不起作用,但如果我使用 .py扩展,它的工作原理。其实扩展并不是那么重要,关键是我是否使用终端来运行脚本,如果我不使用它我有问题,否则
我是一名优秀的程序员,十分优秀!