celery --异步发送短信

## 直接发短信
pip in stall ronglian_sms_sdk


def send_message(mobile, code, time):
    '''

    :param mobile: 手机号
    :param code: 验证码
    :param time: 分钟
    :return:
    '''
    from ronglian_sms_sdk import SmsSDK

    accId = 'xxxxxx'

    accToken = 'xxxxxx'

    appId = 'xxxxx'
    sdk = SmsSDK(accId, accToken, appId)
    tid = '1'
    mobile = mobile
    data = (code, time)
    resp = sdk.sendMessage(tid, mobile, data)
    print(resp)


if __name__ == '__main__':
    # 【云通讯】您的验证码是8888,请于5分钟内正确输入
    send_message('手机号', '8888', '5')

##异步发短信

pip install celery




from celery import Celery

# 创建实例
# celery框架名
import sys
sys.path.insert(0,'D:\demo\p8\opwf_project\celery_task')

celery_app = Celery(
'opwf', 
broker='redis://127.0.0.1:6379/14',#存 14号库
backend='redis://127.0.0.1:6379/15',#结果)  # 15号库


@celery_app.task()
def send_message(tid,mobiles, datas):
    '''

    :param mobile: 手机号
    :param code: 验证码
    :param time: 分钟
    :return:
    '''
    from ronglian_sms_sdk import SmsSDK

    accId = 'xxx'

    accToken = 'xxxx'

    appId = 'xxxx'
    sdk = SmsSDK(accId, accToken, appId)

    resp = sdk.sendMessage(tid, mobiles, datas)
    print(resp)
测试
from celery_task.main import send_message
# send_message('1','13939389145',('2344',2))
send_message.delay('1','13939389145',('2344',2))
1from celery_task.main import send_message
	启动celery -A celery_task.main worker -l info
2from main import send_message
	启动celery -A main worker -l info

你可能感兴趣的:(python,python基础,python)