celery 守护进程部署之后 django 项目所遇问题

2017-04-07 19:50:12 +08:00
 fanne

部署 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 文件也没有生成。

这是什么个情况的

4555 次点击
所在节点    Django
5 条回复
jkmmmm
2017-04-08 08:16:45 +08:00
你从其他地方调用这个 call_shell 没?另外 console 和 celery 日志里就不应该有这些信息吧,用用 logging 模块。
gulu
2017-04-08 09:35:23 +08:00
print 的东西只有在 celery 控制台才看得到的吧,而且还要设置 --loglevel=info
然后你再去看看 error log
fanne
2017-04-08 10:05:04 +08:00
@jkmmmm 是在其他地方调用了这个 call_shell 内容的
call_shell.delay(ipList,add_ipList,shell_dir,shell_file)
fanne
2017-04-08 10:07:08 +08:00
@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 这个文件,但也没有生成。
fanne
2017-04-08 11:24:39 +08:00
@gulu @jkmmmm 是我太大意了,代码问题
shell_log = 'static/searchlog/add_game_log_%s.txt' % nowTime
%nowTime 中间带了个空格,下面一些也是,都带了空格了,改正了一下就正常了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/353314

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX