def kirim(formargs): from tasks import sendmail recipient = formargs.get("to", "*****@*****.**") subject = formargs.get("subject", "hello from flaskCelery") message = formargs.get("message", "ini body email") sendmail.delay(recipient, subject, message)
def regist(): if request.method == 'GET': return render_template('regist.html') elif request.method == 'POST': email = request.form.get("username") password = request.form.get("password") password2 = request.form.get("password2") error = None if not email: error = '用户名不能为空' elif not password: error = '密码不能为空' elif not password2: error = '重复密码不能为空' elif password != password2: error = '密码不一致' if error: flash(error, category="error") return redirect('/regist') else: with sqlite3.connect('demo5.db') as con: cur = con.cursor() cur.execute('select * from user where email = ?', (email, )) r = cur.fetchall() print(r) if len(r) > 0: flash('用户已存在') return redirect('/regist') else: try: from flask_mail import Message from .utils import mail security_password = generate_password_hash(password) cur.execute( 'insert into user (email,password) values (?,?)', (email, security_password)) cur.execute('select id from user where email = ?', (email, )) id = cur.fetchone()[0] print(id, '这是邮箱') serUtil = TimedJSONWebSignatureSerializer( current_app.secret_key, expires_in=24 * 60 * 60) serstr = serUtil.dumps({'id': id}).decode('utf-8') from tasks import sendmail sendmail.delay((email, serstr)) con.commit() return '注册成功' except Exception as e: print(e) return '出异常了'
from tasks import sendmail sendmail.delay(dict(to='*****@*****.**'))
from tasks import sendmail import time while 1: a = sendmail.delay("abc") time.sleep(1) if a.ready(): print a.get()
def home(request): sendmail.delay('*****@*****.**') data = list(Blog.objects.values('caption')) return HttpResponse(json.dumps(data), content_type='application/json')
def regist(): if request.method == "GET": # return "请先登录" flash({}) return render_template('regist.html') if request.method == "POST": username = request.form.get("username") password = request.form.get("password") password2 = request.form.get("password2") error = None if not username: error = "用户名必填" elif not password: error = "密码必填" elif not password2: error = "重复密码必填" elif password != password2: error = "密码不一致" else: with sqlite3.connect("demo5.db") as con: cur = con.cursor() cur.execute("select * from user where username = ?", (username, )) r = cur.fetchall() print(r) if len(r) == 0: print("可以创建") try: # 密码加密 security_password = generate_password_hash(password) cur.execute( "insert into user (username,password) values (?,?)", (username, security_password)) cur.execute("select * from user where username = ?", (username, )) r2 = cur.fetchone() print(r2[0]) # 发送邮件 # 邮箱加密 密钥使用自动产生的密钥 print(current_app.secret_key) seria_util = TimedJSONWebSignatureSerializer( current_app.secret_key, expires_in=3600) serstr = seria_util.dumps({ "id": r2[0] }).decode("utf-8") # msg = Message(subject="神秘组织激活邮件", recipients=[username]) # # msg.html = "<a href='http://127.0.0.1:5000/active/" + str(r2[0]) + "'>点击激活</a>" # msg.html = "<a href='http://127.0.0.1:5000/active/%s'>点击激活</a>" % (serstr,) # mail.send(msg) from tasks import sendmail sendmail.delay(username, serstr) # 发送邮件成功再提交 con.commit() # 邮箱发送失败,不写入数据库 except Exception as e: print(e) con.rollback() return "出现异常" return redirect('/login') error = "用户名已存在" if error: # flash(error, category=error) flash({"error": error, "username": username}) return redirect('/regist')
# -*- coding: utf-8 -*- # @ Author: huangk # @ Time : 2019/9/5 14:16 import time from server import add from tasks import subject, sendmail # 一般用法 # print(sendmail.delay(dict(to='*****@*****.**'))) answer = sendmail.delay(dict(to='*****@*****.**')) while 1: print('wait for ready') if answer.ready(): break time.sleep(0.5) print(answer.get())
import time from celery import group, chain, chord from tasks import sendmail, add, take, no_argument, xsum # 默认一条celery队列,但是在任务执行的过程中,因为只有一条队列,所以任务执行是同步的。 result = sendmail.delay(dict(to='www.com')) # result1 = sendmail.apply_async(args=(dict(to='*****@*****.**'), )) # result2 = add.apply_async(args=(2, 3)) # 多条队列同步进行,需要开启多个路由, 需要在后面传入队列参数queue result3 = take.apply_async(args=(10, 1), queue='web_tasks') # 关联任务, 将前一个任务的结果作为参数传入下一个任务 result4 = add.apply_async(args=(2, 2), link=add.s(3), queue='default') # # 关联任务, 将前一个任务的结果作为参数传入下一个任务 # result5 = add.apply_async(args=(2, 2), link=add.s(4), queue='default') # # 关联任务, 将前一个任务的结果作为参数传入下一个任务 result6 = add.apply_async(args=(2, 2), link=no_argument.si(), queue='default') # # 关联任务, 将前一个任务的结果作为参数传入下一个任务 result7 = add.apply_async(args=(2, 2), link=no_argument.signature(immutable=True), queue='default') # 过期时间 result8 = add.apply_async(args=(2, 3), expires=10, queue='default') # 并行调度, 结果返回列表 result9 = group(add.s(i, i) for i in range(10))(queue='default') # 串行调度, 结果16 result10 = chain(add.s(2, 2), add.s(4), add.s(8))() # chord - 带回调的 group #result11 = chord((add.s(i, i) for i in range(10)), xsum.s())(queue='default')