当前位置: 首页 > news >正文

公司网站建设手续软文新闻发稿平台

公司网站建设手续,软文新闻发稿平台,河南省住房城乡与建设厅网站,创新的网站建设Celery 常用于 web 异步任务、定时任务等。 使用 redis 作为 Celery的「消息代理 / 消息中间件」。 这里通过Flask-Mail使用qq邮箱延时发送邮件作为示例 pip install celery pip install redis pip install Flask-Mail1、使用flask发送邮件 使用 Flask-Mail 发送邮件需要进行…

Celery 常用于 web 异步任务、定时任务等。
使用 redis 作为 Celery的「消息代理 / 消息中间件」。
这里通过Flask-Mail使用qq邮箱延时发送邮件作为示例

pip install celery
pip install redis
pip install Flask-Mail

1、使用flask发送邮件

使用 Flask-Mail 发送邮件需要进行一下配置,其中QQ邮箱授权码的获取方式如下所述:

app = Flask(__name__)
app.config['SECRET_KEY'] = 'top-secret!'# Flask-Mail configuration
app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 465
# 启用/禁用传输安全层加密
app.config['MAIL_USE_TLS'] = False
# 启用/禁用安全套接字层加密
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = '我的QQ邮箱@qq.com'
app.config['MAIL_PASSWORD'] = '我的QQ邮箱授权码'
app.config['MAIL_DEFAULT_SENDER'] = '我的QQ邮箱@qq.com'# Celery configuration
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'# Initialize extensions
mail = Mail(app)@app.route("/send_mail")
def index11():# sender:发件人    recipients:收件人msg = Message('Hello', sender = app.config['MAIL_DEFAULT_SENDER'], recipients = ['目标邮箱@qq.com'])msg.body = "来自python--flask框架发送的邮件内容~"mail.send(msg)#发送Message类对象的内容return "发送成功"

点进qq邮箱,在设置里面点击账号,向下滚动开启pop3服务获取授权码。

2、延时发送邮件

定义celery任务,与flask基本一样 只是前面多了修饰符@celery.task

@celery.task
def send_async_email(email_data):"""Background task to send an email with Flask-Mail."""msg = Message(email_data['subject'],sender=app.config['MAIL_DEFAULT_SENDER'],recipients=[email_data['to']])msg.body = email_data['body']with app.app_context():mail.send(msg)     
@app.route('/', methods=['GET', 'POST'])
def index():if request.method == 'GET':return render_template('index.html', email=session.get('email', ''))email = request.form['email']session['email'] = email# send the emailemail_data = {'subject': 'Hello from Flask','to': email,'body': '来自python--flask框架延时发送的邮件内容~'}if request.form['submit'] == 'Send':# send right awaysend_async_email.delay(email_data)print('here!--')flash('Sending email to {0}'.format(email))else:# send in one minutesend_async_email.apply_async(args=[email_data], countdown=60)flash('An email will be sent to {0} in one minute'.format(email))return redirect(url_for('index'))

3、生成带有状态信息进度条的异步任务


# bind为True,会传入self给被装饰的方法
@celery.task(bind=True)
def long_task(self):"""带有进度条以及状态报告的 异步任务"""verb = ['正在', '准备', '目前', '处于', '进行']adjective = ['全速', '努力', '默默地', '认真', '快速']noun = ['打开', '启动', '修复', '加载', '检查']message = ''total = random.randint(10, 50)  # 随机取10~50的一个随机数for i in range(total):selectnow = random.random()print(selectnow)# 拼接上面三个lsit  随机的生成一些状态描述if not message or selectnow < 0.25:message = '{0} {1} {2}...'.format(random.choice(verb),random.choice(adjective),random.choice(noun))# 更新Celery任务状态self.update_state(state='PROGRESS',meta={'current': i, 'total': total,'status': message})time.sleep(1)# 返回字典return {'current': 100, 'total': 100, 'status': '任务完成!','result': 42}@app.route('/longtask', methods=['POST'])
def longtask():task = long_task.apply_async()return jsonify({}), 202, {'Location': url_for('taskstatus', task_id=task.id)}@app.route('/status/<task_id>')
def taskstatus(task_id):task = long_task.AsyncResult(task_id)# print(task.state)if task.state == 'PENDING':# PENDING的时候  如果一直PENDING可能是celery没开启response = {'state': task.state,'current': 0,'total': 1,'status': 'Pending...'}elif task.state != 'FAILURE':# 加载的时候response = {'state': task.state,'current': task.info.get('current', 0),'total': task.info.get('total', 1),'status': task.info.get('status', '')}if 'result' in task.info:response['result'] = task.info['result']else:# 报错时候的输出response = {'state': task.state,'current': 1,'total': 1,'status': str(task.info),  # this is the exception raised}return jsonify(response)

4、完整代码

文件结构

--- current--- templates--- index.html--- asyn_001.py
这个是asyn_001.py
import os
import random
import time
from flask import Flask, request, render_template, session, flash, redirect, \url_for, jsonify
from flask_mail import Mail, Message
from celery import Celeryapp = Flask(__name__)
app.config['SECRET_KEY'] = 'top-secret!'# Flask-Mail configuration
app.config['MAIL_SERVER'] = 'smtp.qq.com'
app.config['MAIL_PORT'] = 465
# 启用/禁用传输安全层加密
app.config['MAIL_USE_TLS'] = False
# 启用/禁用安全套接字层加密
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = '我的QQ邮箱@qq.com'
app.config['MAIL_PASSWORD'] = '我的QQ邮箱授权码'
app.config['MAIL_DEFAULT_SENDER'] = '我的QQ邮箱@qq.com'# Celery configuration
app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0'# Initialize extensions
mail = Mail(app)@app.route("/send_mail")
def index11():# sender:发件人    recipients:收件人msg = Message('Hello', sender = app.config['MAIL_DEFAULT_SENDER'], recipients = ['目标邮箱@qq.com'])msg.body = "来自python--flask框架发送的邮件内容~"mail.send(msg)#发送Message类对象的内容return "发送成功"# Initialize Celery
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)@celery.task
def send_async_email(email_data):"""Background task to send an email with Flask-Mail."""msg = Message(email_data['subject'],sender=app.config['MAIL_DEFAULT_SENDER'],recipients=[email_data['to']])msg.body = email_data['body']with app.app_context():mail.send(msg)@app.route('/', methods=['GET', 'POST'])
def index():if request.method == 'GET':return render_template('index.html', email=session.get('email', ''))email = request.form['email']session['email'] = email# send the emailemail_data = {'subject': 'Hello from Flask','to': email,'body': '来自python--flask框架延时发送的邮件内容~'}if request.form['submit'] == 'Send':# send right awaysend_async_email.delay(email_data)print('here!--')flash('Sending email to {0}'.format(email))else:# send in one minutesend_async_email.apply_async(args=[email_data], countdown=60)flash('An email will be sent to {0} in one minute'.format(email))return redirect(url_for('index'))# bind为True,会传入self给被装饰的方法
@celery.task(bind=True)
def long_task(self):"""带有进度条以及状态报告的 异步任务"""verb = ['正在', '准备', '目前', '处于', '进行']adjective = ['全速', '努力', '默默地', '认真', '快速']noun = ['打开', '启动', '修复', '加载', '检查']message = ''total = random.randint(10, 50)  # 随机取10~50的一个随机数for i in range(total):selectnow = random.random()print(selectnow)# 拼接上面三个lsit  随机的生成一些状态描述if not message or selectnow < 0.25:message = '{0} {1} {2}...'.format(random.choice(verb),random.choice(adjective),random.choice(noun))# 更新Celery任务状态self.update_state(state='PROGRESS',meta={'current': i, 'total': total,'status': message})time.sleep(1)# 返回字典return {'current': 100, 'total': 100, 'status': '任务完成!','result': 42}@app.route('/longtask', methods=['POST'])
def longtask():task = long_task.apply_async()return jsonify({}), 202, {'Location': url_for('taskstatus', task_id=task.id)}@app.route('/status/<task_id>')
def taskstatus(task_id):task = long_task.AsyncResult(task_id)# print(task.state)if task.state == 'PENDING':# PENDING的时候  如果一直PENDING可能是celery没开启response = {'state': task.state,'current': 0,'total': 1,'status': 'Pending...'}elif task.state != 'FAILURE':# 加载的时候response = {'state': task.state,'current': task.info.get('current', 0),'total': task.info.get('total', 1),'status': task.info.get('status', '')}if 'result' in task.info:response['result'] = task.info['result']else:# 报错时候的输出response = {'state': task.state,'current': 1,'total': 1,'status': str(task.info),  # this is the exception raised}return jsonify(response)if __name__ == '__main__':app.run(debug=True)
这个是index.html
<html><head><title>Flask + Celery 示例</title><style>.progress {width: 100%;text-align: center;}</style></head><body><h1>Flask + Celery 示例</h1><h2>Example 1: 发送异步邮件</h2>{% for message in get_flashed_messages() %}<p style="color: red;">{{ message }}</p>{% endfor %}<form method="POST"><p>Send test email to: <input type="text" name="email" value="{{ email }}"></p><input type="submit" name="submit" value="Send"><input type="submit" name="submit" value="Send in 1 minute"></form><hr><h2>Example 2: 生成进度条以及状态报告</h2><!--<button οnclick="start_long_task();">Start Long Calculation</button><br><br>--><button id="start-bg-job">Start Long Calculation</button><br><br><div id="progress"></div><script src="//cdnjs.cloudflare.com/ajax/libs/nanobar/0.2.1/nanobar.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><script>function start_long_task() {// add task status elementsdiv = $('<div class="progress"><div></div><div>0%</div><div>...</div><div>&nbsp;</div></div><hr>');$('#progress').append(div);// create a progress barvar nanobar = new Nanobar({bg: '#44f',target: div[0].childNodes[0]});// send ajax POST request to start background job$.ajax({type: 'POST',url: '/longtask',success: function(data, status, request) {status_url = request.getResponseHeader('Location');console.log("status_url", status_url,"nanobar", nanobar, "div[0]", div[0])console.log("data", data)update_progress(status_url, nanobar, div[0]);},error: function() {alert('Unexpected error');}});}function update_progress(status_url, nanobar, status_div) {// send GET request to status URL$.getJSON(status_url, function(data) {// update UIpercent = parseInt(data['current'] * 100 / data['total']);nanobar.go(percent);$(status_div.childNodes[1]).text(percent + '%');$(status_div.childNodes[2]).text(data['status']);if (data['state'] != 'PENDING' && data['state'] != 'PROGRESS') {if ('result' in data) {// show result$(status_div.childNodes[3]).text('Result: ' + data['result']);}else {// something unexpected happened$(status_div.childNodes[3]).text('Result: ' + data['state']);}}else {// rerun in 2 secondssetTimeout(function() {update_progress(status_url, nanobar, status_div);}, 2000);}});}$(function() {$('#start-bg-job').click(start_long_task);});</script></body>
</html>

5、启动任务

终端cd到current文件夹所在目录
在这里插入图片描述
启动asyn_001程序,即可观察到异步任务的执行。

参考1 Celery实现异步任务和定时任务的简单示例
参考2 Using Celery with Flask


文章转载自:
http://wanjiacribbage.rbzd.cn
http://wanjiaphotographic.rbzd.cn
http://wanjiaphycology.rbzd.cn
http://wanjiaexclusion.rbzd.cn
http://wanjiabraggart.rbzd.cn
http://wanjiacontinuator.rbzd.cn
http://wanjiapolluted.rbzd.cn
http://wanjiaflex.rbzd.cn
http://wanjiaalgonquin.rbzd.cn
http://wanjiahyperphysically.rbzd.cn
http://wanjiacarcass.rbzd.cn
http://wanjiaobituarist.rbzd.cn
http://wanjiaportray.rbzd.cn
http://wanjiaarthroscope.rbzd.cn
http://wanjiakerf.rbzd.cn
http://wanjiatimbales.rbzd.cn
http://wanjiamoribund.rbzd.cn
http://wanjiaundertaking.rbzd.cn
http://wanjiafilariid.rbzd.cn
http://wanjialight.rbzd.cn
http://wanjiapaleomagnetism.rbzd.cn
http://wanjiavugular.rbzd.cn
http://wanjiabopeep.rbzd.cn
http://wanjiaserotonin.rbzd.cn
http://wanjiathrowaway.rbzd.cn
http://wanjiaacrogenous.rbzd.cn
http://wanjiaruana.rbzd.cn
http://wanjialeatherware.rbzd.cn
http://wanjiaserially.rbzd.cn
http://wanjiabiphenyl.rbzd.cn
http://wanjiasmallpox.rbzd.cn
http://wanjiaoverdaring.rbzd.cn
http://wanjiakenosis.rbzd.cn
http://wanjiaspaceway.rbzd.cn
http://wanjiadioxide.rbzd.cn
http://wanjiacomplaining.rbzd.cn
http://wanjiaploughhead.rbzd.cn
http://wanjiaoctopodes.rbzd.cn
http://wanjiayouthhood.rbzd.cn
http://wanjiasomesthetic.rbzd.cn
http://wanjiaportosystemic.rbzd.cn
http://wanjiatrochal.rbzd.cn
http://wanjiayamal.rbzd.cn
http://wanjiatetracaine.rbzd.cn
http://wanjiaunmix.rbzd.cn
http://wanjiaheadborough.rbzd.cn
http://wanjialinksland.rbzd.cn
http://wanjiasplenitis.rbzd.cn
http://wanjiastringless.rbzd.cn
http://wanjiaoutsize.rbzd.cn
http://wanjiaolio.rbzd.cn
http://wanjiacriminaloid.rbzd.cn
http://wanjiachrysarobin.rbzd.cn
http://wanjiamaintainor.rbzd.cn
http://wanjiasnipe.rbzd.cn
http://wanjiadiphtherial.rbzd.cn
http://wanjiatrashy.rbzd.cn
http://wanjialaminated.rbzd.cn
http://wanjianalorphine.rbzd.cn
http://wanjiabullterrier.rbzd.cn
http://wanjiapercale.rbzd.cn
http://wanjiaeradiate.rbzd.cn
http://wanjiarestitution.rbzd.cn
http://wanjiatokonoma.rbzd.cn
http://wanjiauninhabited.rbzd.cn
http://wanjiasilage.rbzd.cn
http://wanjiatalebearing.rbzd.cn
http://wanjiaostiak.rbzd.cn
http://wanjiaheptaglot.rbzd.cn
http://wanjiawri.rbzd.cn
http://wanjiacholecystagogue.rbzd.cn
http://wanjiafauxbourdon.rbzd.cn
http://wanjiainefficacy.rbzd.cn
http://wanjiapositive.rbzd.cn
http://wanjiasphenographic.rbzd.cn
http://wanjiameteorologist.rbzd.cn
http://wanjiarenegado.rbzd.cn
http://wanjiatroll.rbzd.cn
http://wanjiadressmake.rbzd.cn
http://wanjiaabstinence.rbzd.cn
http://www.15wanjia.com/news/111379.html

相关文章:

  • 怎么做草坪网站网站维护的主要内容
  • wordpress主题工作室seo技巧优化
  • 农场游戏系统开发 网站建设推广免费制作自己的网站
  • php动态网站开发案例教程pdf网站搜索引擎优化工具
  • drupal 和wordpress路由器优化大师
  • 百度指数做网站网站搭建教程
  • 大网站百度搜索关键词技巧
  • 网站设计制造码迷seo
  • 网站功能简介做关键词推广
  • 深圳seo网站优化公司seo超级外链
  • 网站建设存在不足it培训学校it培训机构
  • 网站音乐播放器代码百度旗下的所有产品
  • 网站构建的友情链接怎么做企业官网首页设计
  • asp响应式h5网站源码nba西部排名
  • 武汉搭建网站网页设计模板网站免费
  • 网站设计专业有前途吗家庭优化大师
  • 地方购物网站盈利模式最新新闻播报
  • 网站建设重要新关键一招
  • 网站开发服务windows优化大师win10
  • 郑州的兼职网站建设天津抖音seo
  • 服装设计参考网站平面设计正规培训机构
  • 购物网站有哪些?汽车营销策划方案ppt
  • 导航网站是怎么做的短视频关键词优化
  • 进博会上海广州seo工作
  • 网站怎么做百度口碑贵阳网络推广外包
  • 如何做淘宝联盟网站的推广百度搜索关键词指数
  • 蛋糕店网站开发策划书南宁网站建设公司排行
  • 浅谈电子商务网站的建设与管理网络推广100种方法
  • 微信做的团购网站企业网站建设步骤
  • 网站建设中...优化的概念