V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fanne
V2EX  ›  Django

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

  •  
  •   fanne · 2017-04-07 19:50:12 +08:00 · 4519 次点击
    这是一个创建于 2547 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    这是什么个情况的

    5 条回复    2017-04-08 11:24:39 +08:00
    jkmmmm
        1
    jkmmmm  
       2017-04-08 08:16:45 +08:00
    你从其他地方调用这个 call_shell 没?另外 console 和 celery 日志里就不应该有这些信息吧,用用 logging 模块。
    gulu
        2
    gulu  
       2017-04-08 09:35:23 +08:00 via Android
    print 的东西只有在 celery 控制台才看得到的吧,而且还要设置 --loglevel=info
    然后你再去看看 error log
    fanne
        3
    fanne  
    OP
       2017-04-08 10:05:04 +08:00
    @jkmmmm 是在其他地方调用了这个 call_shell 内容的
    call_shell.delay(ipList,add_ipList,shell_dir,shell_file)
    fanne
        4
    fanne  
    OP
       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
        5
    fanne  
    OP
       2017-04-08 11:24:39 +08:00
    @gulu @jkmmmm 是我太大意了,代码问题
    shell_log = 'static/searchlog/add_game_log_%s.txt' % nowTime
    %nowTime 中间带了个空格,下面一些也是,都带了空格了,改正了一下就正常了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5446 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 06:43 · PVG 14:43 · LAX 23:43 · JFK 02:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.