Python 我想将gevent.evnet用于芹菜.task
我一直在研究长时间的投票系统。我用烧瓶+芒果+芹菜+gevent 当芹菜任务中的进程完成时,gevent.event.set()不起作用。因此,我想帮助解决这个问题。(为什么我同时使用gevent和芹菜,在通知系统中需要处理大量的过程) 这是我的示例代码Python 我想将gevent.evnet用于芹菜.task,python,flask,celery,long-polling,gevent,Python,Flask,Celery,Long Polling,Gevent,我一直在研究长时间的投票系统。我用烧瓶+芒果+芹菜+gevent 当芹菜任务中的进程完成时,gevent.event.set()不起作用。因此,我想帮助解决这个问题。(为什么我同时使用gevent和芹菜,在通知系统中需要处理大量的过程) 这是我的示例代码 #server.py @celery.task() def doing_task(uid, message): notification = Notification() # this is a notification Mod
#server.py
@celery.task()
def doing_task(uid, message):
notification = Notification() # this is a notification Model
notification.add(request.args.get('to'), some_notification)
app.event.set()
app.event.clear()
@app.route('/main')
def main():
return render_template('main.html')
@app.route('/set')
def set():
doing_task.delay(request.args.get('uid'), 'Notify')
return 'OK'
@app.route('/poll')
def poll():
uid = request.args.get('uid')
app.event.wait()
if is_authorized(uid): #uid 1 is a authorized account
return Notification().get(uid)
#main.html
<body>
<button>Click me</button>
</body>
<script>
$('button').click(function(e) {
$.ajax({
'url': '/set',
'data': 'uid=1',
'success': function(data) {
console.log(data);
}
});
e.preventDefault();
});
var poll = function() {
return $.ajax({
'url': '/poll',
'method': 'get',
'async':true,
'dataType': 'json',
'timeout': 10000,
'success': function(data) {
console.log(data);
setTimeout(poll, 50);
},
'error':function (req,sta,er){
setTimeout(poll, 3000);
},
});
};
poll()
</script>
#server.py
@芹菜
def正在执行任务(uid,消息):
通知=通知()#这是一个通知模型
notification.add(request.args.get('to'),some_通知)
app.event.set()
app.event.clear()
@应用程序路径(“/main”)
def main():
返回呈现模板('main.html')
@应用程序路径(“/set”)
def set():
正在执行任务。延迟(request.args.get('uid'),'Notify')
返回“OK”
@app.route(“/poll”)
def poll():
uid=request.args.get('uid')
app.event.wait()
如果已授权(uid):#uid 1是授权帐户
返回通知().get(uid)
#main.html
点击我
$(“按钮”)。单击(函数(e){
$.ajax({
“url”:“/set”,
“数据”:“uid=1”,
“成功”:函数(数据){
控制台日志(数据);
}
});
e、 预防默认值();
});
var poll=function(){
返回$.ajax({
“url”:“/poll”,
'方法':'获取',
“异步”:true,
“数据类型”:“json”,
“超时”:10000,
“成功”:函数(数据){
控制台日志(数据);
设置超时(轮询,50);
},
“错误”:函数(请求、sta、er){
设置超时(轮询,3000);
},
});
};
投票()
现在,在Flask 0.9Flask.app\u context
中添加了Flask.app\u context
,您可以获得当前的上下文
看
比如说,
from flask import Flask
from celery import Celery
app = Flask(__name__)
celery = Celery(__name__)
@celery.task
def hello():
# Also, you are able to deal with current request as use test_request_context
with app.app_context():
print current_app
with app.test_request_context() as request:
print('Hello {0!r}'.format(request))