2012-07-16 27 views
5

में करना चाहता हूं, मैं लंबी-मतदान प्रणाली पर काम कर रहा हूं। मैं फ्लास्क + मोंगोकिट + अजवाइन + गीवेंट का उपयोग करता हूं।मैं gevent.evnet का उपयोग celery.task

जब सेलेरी कार्य में प्रक्रिया होती है, तो gevent.event.set() काम नहीं किया जाता है। तो मैं इसे समझने में मदद करना चाहता हूं। (कारण मैं सेलेरी के साथ एक ही समय में भूगर्भ का उपयोग करता हूं, अधिसूचना प्रणाली में निपटने के लिए बड़ी प्रक्रिया है)

यहां मेरा नमूना कोड है।

#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> 

उत्तर

4

अब, बोतल 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)) 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^