2013-03-03 35 views
6

所以,基本上我想要構建一個在Heroku上使用RQ的長輪詢應用程序。我已經看過這個問題Flask: passing around background worker job (rq, redis)但它沒有幫助。如何通過RQ python中的id獲取作業?

這基本上是我在做什麼。

@app.route('/do_something', methods=['POST']) 
def get_keywords(): 
    data_json = json.loads(request.data) 
    text = urllib.unquote(data_json["sentence"]) 

    job = q.enqueue(keyword_extraction.extract, text) 
    return job.key 

@app.route('/do_something/<job_id>', methods=['GET']) 
def get_keywords_results(job_id): 
    job = Job().fetch(job_id) 

    if(not job.is_finished): 
     return "Not yet", 202 
    else: 
     return str(job.result) 

沒有什麼花哨,所以當POST請求到來時,它將隊列中的作業並返回作業ID用戶immidiately,然後用戶將使用鍵保持投票的結果。不過,我似乎無法得到這個工作,因爲這行Job().fetch(job_id)回報

NoRedisConnectionException: Could not resolve a Redis connection.

任何幫助將非常感激。

+0

你是否安裝了redis adon並正確設置了它? – CraigKerstiens 2013-03-03 04:57:28

+0

我在本地主機上測試過它。當工作入隊時它工作正常。但是當我嘗試獲取Job().fetch(job_id)時,它會給出我的錯誤。 – toy 2013-03-03 09:57:22

回答

11

我已經發現了這一點,以防有人感興趣。它必須是這個。

Job.fetch(job_id, connection=conn) 
+0

作業現在在API中不可用:(你知道任何替代方法嗎? – 2015-03-02 08:48:22

+2

啊,找到'q.fetch_job(job_id)'。 – 2015-03-02 09:10:57

0

這是由於現在已經修復的迴歸,詳情請參閱https://github.com/nvie/rq/issues/479

爲了解決這個問題,你需要從github主分支安裝,直到它發佈到PyPI。