V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
diwuqin
V2EX  ›  Python

使用 celery 创建异步任务时, 发现了调用 task_sms 的时候 delay 并没有执行函数

  •  
  •   diwuqin · 2020-06-09 16:54:47 +08:00 · 1390 次点击
    这是一个创建于 1409 天前的主题,其中的信息可能已经有所发展或是发生改变。

    def send_sms(mobile: str, content: str, templateId: str): """发送短信""" values = { 'accesskey': accesskey, 'secret': secret, 'sign': sign, 'templateId': templateId, 'mobile': mobile, 'content': content, }

    data = urllib.parse.urlencode(values)
    new_url = url + "?" + data
    res = urllib.request.urlopen(new_url)
    res_json = json.loads(res.read().decode('utf-8'))
    return res_json
    

    @celery_app.task(name='task_sms') def task_sms(phone, num): """任务派发短信通知""" result = send_sms(phone, num, task_templateId) print(result) return result

    if name == 'main': import time

    start = time.time()
    send_community_sms.delay("18888888888", '{}##{}'.format('1111111111', '111111111111111'))
    res = send_community_sms.delay("18888888888", '{}##{}'.format('1111111111', '111111111111111'))
    print(res)
    

    config.py from celery import Celery BROKER = 'redis://127.0.0.1:6379/4' BACKEND = 'redis://127.0.0.1:6379/5'

    创建 celery 应用对象

    celery_app = Celery("baoanliezha", broker=BROKER, backend=BACKEND)

    搜索 celery 异步任务

    celery_app.autodiscover_tasks([ "celery_tasks.sms", ])

    最后 res 打印返回 22bd44e2-8eeb-4e6e-aac6-1242b750b602

    3 条回复    2020-06-10 15:11:44 +08:00
    diwuqin
        1
    diwuqin  
    OP
       2020-06-09 16:55:39 +08:00
    我靠怎么没有转行呢....
    zouri
        2
    zouri  
       2020-06-09 17:26:21 +08:00
    你为啥不舍得排版一下代码呢
    diwuqin
        3
    diwuqin  
    OP
       2020-06-10 15:11:44 +08:00
    @zouri 我写的时候好像是排的, 结果发出来成这了...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5236 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:28 · PVG 15:28 · LAX 00:28 · JFK 03:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.