部署 celery 的守护进程
参考资料: http://docs.celeryproject.org/en/latest/userguide/daemonizing.html#daemonizing
拷贝这个文件( https://github.com/celery/celery/blob/3.1/extra/generic-init.d/celeryd )内容到 /etc/init.d/celeryd
编写项目的 celery 配置文件/etc/default/celeryd
cat /etc/default/celeryd
CELERYD_NODES="djangoCelery1"
CELERY_BIN="/usr/local/python27/bin/celery"
CELERY_APP="ansible_ops"
CELERYD_CHDIR="/my/project"
CELERYD_OPTS="--time-limit=300 --concurrency=4"
CELERY_LOG_LEVEL="DEBUG"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_USER="root"
CELERYD_GROUP="root"
CELERY_CREATE_DIRS=1
启动后一些日志输出:
[2017-04-07 19:36:50,601: INFO/MainProcess] Received task: items.views.call_shell[c5b52a72-f6da-4b32-8f4e-3b843601f5cc]
[2017-04-07 19:44:39,190: INFO/MainProcess] Connected to redis://localhost:6379/0
[2017-04-07 19:44:39,197: INFO/MainProcess] mingle: searching for neighbors
[2017-04-07 19:44:40,213: INFO/MainProcess] mingle: all alone
[2017-04-07 19:44:40,226: WARNING/MainProcess] /usr/local/python27/lib/python2.7/site-packages/celery/fixups/django.py:202: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '
这个算是把 celery 用守护进程部署好了。
django 直接用 python manage.py runserver 启动在 console 控制台上。
用守护进程部署好 celery 后所遇到以下问题 django 的代码片段
@celery_app.task
def call_shell(ipList,add_ipList,shell_dir,shell_file):
nowTime = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(time.time()))
print 'dddddddddddddddddddddddddddddd'
print 'dddddddddddddddddddddddddddddd'
print nowTime
shell_log = 'static/searchlog/add_game_log_%s.txt' % nowTime
os.popen(r"echo %s >> %s" % (ipList, add_ipList))
p = Popen("cd %s;/bin/sh %s" % (shell_dir, shell_file),stdout=PIPE,stderr=STDOUT,shell=True,bufsize=1)
for line in iter(p.stdout.readline,b''):
shell_log.write(line)
print line
p.stdout.close()
os.popen(r"/bin/sed -i '$d' %s" % add_ipList)
当 celery 用守护进程部署好之后,代码里的
nowTime = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(time.time()))
print 'dddddddddddddddddddddddddddddd'
print 'dddddddddddddddddddddddddddddd'
print nowTime
在 console 里和 celery 的日志里都没看到这些 print 信息,然后 shell_log 文件也没有生成。
这是什么个情况的
1
jkmmmm 2017-04-08 08:16:45 +08:00
你从其他地方调用这个 call_shell 没?另外 console 和 celery 日志里就不应该有这些信息吧,用用 logging 模块。
|
2
HFcbyqP0iVO5KM05 2017-04-08 09:35:23 +08:00 via Android
print 的东西只有在 celery 控制台才看得到的吧,而且还要设置 --loglevel=info
然后你再去看看 error log |
3
fanne OP @jkmmmm 是在其他地方调用了这个 call_shell 内容的
call_shell.delay(ipList,add_ipList,shell_dir,shell_file) |
4
fanne OP @gulu
日志没打印出来,然后我的一个文件也没有生成的 nowTime = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(time.time())) shell_log = 'static/searchlog/add_game_log_%s.txt' % nowTime 正常来说应该会根据调用这个 call_shell 的时候时间生成的一个 shell_log 的文件 for line in iter(p.stdout.readline,b''): shell_log.write(line) p.stdout.close() 然后通过 shell_log.write(line)写入到 shell_log 这个文件,但也没有生成。 |