django + celery(守护进程部署) + sendMail 无法发送邮件问题 - V2EX
fanne

django + celery(守护进程部署) + sendMail 无法发送邮件问题

  •  
  •   fanne Mar 17, 2017 4831 views
    This topic created in 3348 days ago, the information mentioned may be changed or developed.

    django+celery+redis+send_mail 来发送邮件

    当直接 console 启动 django 项目和 celery 时候,既如下情况时候可以正常邮件发送:

    启动 django

    [root@vultrvpn vpnWeb]# python run.py Performing system checks... System check identified no issues (0 silenced). March 17, 2017 - 11:09:42 Django version 1.10.6, using settings 'vpnWeb.settings' Starting development server at http://0.0.0.0:9000/ Quit the server with CONTROL-C. [17/Mar/2017 11:11:51] "GET /admin/ HTTP/1.0" 200 6274[17/Mar/2017 11:11:54] "GET /admin/app/userpay/ HTTP/1.0" 200 7052 [17/Mar/2017 11:11:55] "GET /admin/jsi18n/ HTTP/1.0" 200 3217 [17/Mar/2017 11:12:00] "POST /admin/app/userpay/ HTTP/1.0" 200 3168 

    启动 celery

    [root@vultrvpn vpnWeb]# celery -A vpnWeb worker -l info /usr/lib/python2.7/site-packages/celery/platforms.py:793: RuntimeWarning: You're running the worker with superuser privileges: this is absolutely not recommended! Please specify a different user using the -u option. User information: uid=0 euid=0 gid=0 egid=0 uid=uid, euid=euid, gid=gid, egid=egid, -------------- celery@vultrvpn v4.0.2 (latentcall) ---- **** ----- --- * *** * -- Linux-4.10.2-1.el7.elrepo.x86_64-x86_64-with-centos-7.3.1611-Core 2017-03-17 11:09:32 -- * - **** --- - ** ---------- [config] - ** ---------- .> app: vpnWeb:0x234a450 - ** ---------- .> transport: redis://localhost:6379/0 - ** ---------- .> results: redis://localhost:6379/0 - *** --- * --- .> concurrency: 1 (prefork) -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker) --- ***** ----- -------------- [queues] .> celery exchange=celery(direct) key=celery [tasks] . app.emailTask.sendEmal . vpnWeb.celery.debug_task [2017-03-17 11:09:32,548: INFO/MainProcess] Connected to redis://localhost:6379/0 [2017-03-17 11:09:32,557: INFO/MainProcess] mingle: searching for neighbors [2017-03-17 11:09:33,571: INFO/MainProcess] mingle: all alone [2017-03-17 11:09:33,578: INFO/MainProcess] celery@vultrvpn ready. [2017-03-17 11:14:55,514: INFO/MainProcess] Received task: app.emailTask.sendEmal[618eb32c-0c34-4cf7-9c0f-9020f6d72242] [2017-03-17 11:14:58,385: INFO/PoolWorker-1] Task app.emailTask.sendEmal[618eb32c-0c34-4cf7-9c0f-9020f6d72242] succeeded in 2.868786066s: None 

    当项目调用到 send_mail 时候可以正常进行邮件发送

    发送邮件代码

    from django.core.mail import send_mail import time from time import sleep @celery_app.task def sendEmal(tomail): send_mail( u'账号信息', u'发送目标邮箱', '[email protected]', [tomail], fail_silently=False, ) 

    但当用 supervisor 进行守护进程部署 django 以及 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 调用发送邮件 task 的时候打印日志内容:

    [2017-03-17 10:58:30,190: INFO/MainProcess] mingle: all alone [2017-03-17 10:58:30,197: INFO/MainProcess] djangoCelery1@vultrvpn ready. [2017-03-17 10:58:49,185: INFO/MainProcess] Received task: app.emailTask.sendEmal[1c9ff6d0-2962-42ce-ba35-151c73980ffb] [2017-03-17 11:06:51,331: INFO/MainProcess] Received task: app.emailTask.sendEmal[7b71fa6c-0554-44c3-a030-20f14885f001] [2017-03-17 11:07:44,339: INFO/MainProcess] Connected to redis://localhost:6379/0 [2017-03-17 11:07:44,347: INFO/MainProcess] mingle: searching for neighbors [2017-03-17 11:07:45,361: INFO/MainProcess] mingle: all alone [2017-03-17 11:07:45,367: INFO/MainProcess] djangoCelery1@vultrvpn ready. [2017-03-17 11:30:44,493: INFO/MainProcess] Connected to redis://localhost:6379/0 [2017-03-17 11:30:44,499: INFO/MainProcess] mingle: searching for neighbors [2017-03-17 11:30:45,526: INFO/MainProcess] mingle: all alone [2017-03-17 11:30:45,532: INFO/MainProcess] djangoCelery1@vultrvpn ready. [2017-03-17 11:31:04,372: INFO/MainProcess] Received task: app.emailTask.sendEmal[680f3ae0-a85f-4b7c-a9d7-9c64d1788aa2] 

    后有经过测试, django 部署守护进程, celery 用 console 启动,就可以正常发送邮件。

    那么现在要怎么调整才能在 celery 使用守护进程部署的时候也能进行邮件发送的?

    5 replies    2017-03-17 23:27:09 +08:00
    vicalloy
        1
    vicalloy  
       Mar 17, 2017   1
    控制台正常, supervisor 不正常,很多时候都是编码问题引起的。
    你在 supervisor 里指定编码看看。

    [supervisord]
    envirOnment=LANG="en_US.utf8", LC_ALL="en_US.UTF-8", LC_LANG="en_US.UTF-8"
    phithon
        2
    phithon  
       Mar 17, 2017
    控制台正常, supervisor 不正常
    也有可能是工作目录或者环境变量没配。
    fanne
        3
    fanne  
    OP
       Mar 17, 2017
    @vicalloy 是环境变量问题,我的发送邮箱的账号密码写入到 /etc/profile 中了
    用了你这个方式 就可以加载到账号密码变量了。
    多谢哈。
    fanne
        4
    fanne  
    OP
       Mar 17, 2017
    @phithon 是的,环境变量出问题了。
    lc4t
        5
    lc4t  
       Mar 17, 2017
    django-asyncmailer
    About     Help     Advertise     Blog     API     FAQ     Solana     846 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 43ms UTC 21:06 PVG 05:06 LAX 14:06 JFK 17:06
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86