django - 尝试使用 Redis 队列在 heroku 上的 django 中进行一些异步处理

标签 django heroku redis worker

我正在尝试将更大的文件异步上传到 s3。我已经关注了来自 http://python-rq.org/ 的文档. Worker 正在运行,但没有完成工作。

View .py

 def youmaterial(request):

    if request.method == 'POST':

        newdoc = Youmaterial(docfile = request.FILES['file'],user=request.user)
        newdoc.save()
        msg='dee'
            # Redirect to the document list after POST
        return HttpResponse(json.dumps({'message': msg}))

    else:
        form1 = DocumentForm()
    return render(request,'mat_upload.html',{'form':form1})

def create_job():
    redis_conn = Redis()
    q = Queue(connection=redis_conn)  # no args implies the default queue

# Delay execution of count_words_at_url('http://nvie.com')
    job = q.enqueue(youmaterial, 'http://heroku.com')

worker .py

import os
import urlparse
from redis import Redis
from rq import Worker, Queue, Connection

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDISTOGO_URL')
if not redis_url:
    raise RuntimeError('Set up Redis To Go first.')

urlparse.uses_netloc.append('redis')
url = urlparse.urlparse(redis_url)
conn = Redis(host=url.hostname, port=url.port, db=0, password=url.password)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()

过程:

web: gunicorn gettingstarted.wsgi --log-file -
worker: python -u worker.py

Settings.py(这给了我错误。)

DJANGO_SETTINGS_MODULE=config.settings rqworker high default low  

最佳答案

AFAIK,您不能以这种方式对整个 View 函数进行排队(需要一个 request 对象,而您传递一个 str)。

您是否正在尝试实现某种异步处理 View ?相反,您应该确定长处理函数并将作业放入 View 中,更像这样:

def s3_async_upload(obj):
    obj.save()

def youmaterial(request):

    if request.method == 'POST':

        newdoc = Youmaterial(docfile=request.FILES['file'], user=request.user)
        # Replace newdoc.save()
        q.enqueue(s3_async_upload, obj=newdoc)
        msg = 'dee'
        # Redirect to the document list after POST
        return HttpResponse(json.dumps({'message': msg}))

    else:
        form1 = DocumentForm()

    return render(request, 'mat_upload.html', {'form': form1})

# or with the `@job` decorator, usage: s3_async_upload(obj=newdoc)

@job
def s3_async_upload(obj):
    obj.save()

也许其他设置会丢失,但希望对您有所帮助!

关于django - 尝试使用 Redis 队列在 heroku 上的 django 中进行一些异步处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39062282/

相关文章:

python - 在 Django 1.9 中更改站点名称

heroku - 配置在 Parse Server 仪表板上不可见?

django - 如何使用 Heroku 和 Django 实现简单的 cron 作业

.NET Stackexchange.Redis : conditional delete depending on value?

ruby-on-rails - 在 Ruby 和 Redis 中匹配现场玩家的最佳策略?

django - 我应该将一个大的 Django 项目拆分为多个应用程序吗?

python - 模型中的 Django 循环导入

python - 我可以向 Django auth_group 表添加一列吗?

ruby-on-rails - 如何使用 Foreman 正确关闭和转储 Redis 服务器?

git - 推送到 Heroku 时出现 SSL 证书错误