我有一个简单的Celery任务,如下所示:
@celery.task
def tail():
cmd = 'nslookup www.google.com'
cm = shlex.split(cmd)
out = subprocess.check_output(cm)
print out
当工作人员执行任务时,会输出类似的内容:
[2013-12-02 23:54:26,007: WARNING/Worker-2]
Server: 192.168.1.254
Address: 192.168.1.254#53
Non-authoritative answer:
Name: www.google.com
Address: 173.194.34.112
Name: www.google.com
Address: 173.194.34.113
Name: www.google.com
Address: 173.194.34.115
Name: www.google.com
Address: 173.194.34.114
Name: www.google.com
Address: 173.194.34.116
[2013-12-02 23:54:26,008: INFO/MainProcess] Task app.tail[23f625f7-d328-47c5-8f57- 90a330ebe100] succeeded in 0.0618259499897s: None
我需要芹菜任务捕获每行的标准输出,以便我可以重定向它并将其存储在其他位置。
我可以在Celery docs http://docs.celeryproject.org/en/latest/reference/celery.app.log.html?highlight=stdout#celery.app.log.Logging.redirect_stdouts中看到定义记录器和重定向stdout的选项,但是我无法弄清楚如何连接它!
建议将不胜感激!
问候。
答案 0 :(得分:0)
这与celery无关,如果你想在捕获输出时执行一个命令,你应该以不同的方式执行它。
查看Subprocess模块:http://docs.python.org/2/library/subprocess.html
具体来说,关于捕获stderr
的部分,因为它没有写入stdout
:)
import celery
import subprocess
@celery.task
def tail():
process = subprocess.Popen('nslookup www.google.com', shell=True, stdout=PIPE, stderr=PIPE)
print 'Stdout: ', process.stdout
print 'Stderr: ', process.stderr