python – 我想将gevent.evnet用于celery.task

我一直在研究长轮询系统.我用烧瓶mongokit芹菜gevent.

当进入芹菜任务时,完成gevent.event.set()不能正常工作.所以我想帮助解决这个问题.(我之所以与celery同时使用gevent,在Notification系统中有很大的处理过程)

这是我的示例代码.

 #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
 
最佳答案
现在,在Flask 0.9中添加了Flask.app_context,使用Flask.app_context可以获得当前的上下文.

Application 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))

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...