我一直在研究长轮询系统.我用烧瓶+ mongokit + celery + 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
<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() …Run Code Online (Sandbox Code Playgroud)