部署 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 文件也没有生成。
这是什么个情况的
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.