0

我有一个长时间运行的函数,我想在 heroku 上作为后台函数运行。我正在使用 redis 和 rq 来管理它。

我的仪表板视图函数将数据加载到会话对象中以:

@login_required
@main.route('/dash', methods=['GET', 'POST'])
def dash():
    if request.method =='POST':
        print(request.form.get('posting'))
        session['posting']=request.form.get('posting')
        session['folder']=request.form.get('folder')

然后在选择后,信息被传递给:

@login_required
@main.route('/confirm', methods=['GET', 'POST'])
def confirm():
    print(session['posting'])
    return foreground_vs_background(confirm_function,[session])

然后:

def foreground_vs_background(func, args):

        j=  get_queue().enqueue(func,args = args,result_ttl=5000)
        return (j.get_id())

最后我传入的函数以:

def confirm_function():
    p =  session['posting']
    folder  =  session['folder']

heroku 日志显示:

2018-03-09T14:43:14.357633+00:00 app[worker.1]:     return self.run(*args, **kwargs)
2018-03-09T14:43:14.357642+00:00 app[worker.1]:   File "manage.py", line 111, in run_worker
2018-03-09T14:43:14.357861+00:00 app[worker.1]:     worker.work()
2018-03-09T14:43:14.357870+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/rq/worker.py", line 460, in work
2018-03-09T14:43:14.360547+00:00 app[worker.1]:     self.execute_job(job, queue)
2018-03-09T14:43:14.360590+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/rq/worker.py", line 575, in execute_job
2018-03-09T14:43:14.360593+00:00 app[worker.1]:     self.fork_work_horse(job, queue)
2018-03-09T14:43:14.360594+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/rq/worker.py", line 521, in fork_work_horse
2018-03-09T14:43:14.360596+00:00 app[worker.1]:     self.main_work_horse(job, queue)
2018-03-09T14:43:14.360598+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/rq/worker.py", line 590, in main_work_horse
2018-03-09T14:43:14.360599+00:00 app[worker.1]:     success = self.perform_job(job, queue)
2018-03-09T14:43:14.360601+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/rq/worker.py", line 692, in perform_job
2018-03-09T14:43:14.360603+00:00 app[worker.1]:     self.prepare_job_execution(job)
2018-03-09T14:43:14.360604+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/rq/worker.py", line 624, in prepare_job_execution
2018-03-09T14:43:14.360610+00:00 app[worker.1]:     self.procline(msg.format(job.func_name, job.origin, time.time()))
2018-03-09T14:43:14.360612+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/rq/job.py", line 241, in func_name
2018-03-09T14:43:14.360842+00:00 app[worker.1]:     self._unpickle_data()
2018-03-09T14:43:14.360848+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/rq/job.py", line 209, in _unpickle_data
2018-03-09T14:43:14.361116+00:00 app[worker.1]:     self._func_name, self._instance, self._args, self._kwargs = unpickle(self.data)
2018-03-09T14:43:14.361121+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/rq/job.py", line 53, in unpickle
2018-03-09T14:43:14.361329+00:00 app[worker.1]:     raise UnpickleError('Could not unpickle', pickled_string, e)
2018-03-09T14:43:14.361396+00:00 app[worker.1]: rq.exceptions.UnpickleError: ('Could not unpickle', RecursionError('maximum recursion depth exceeded while calling a Python object',)
)
2018-03-09T14:43:14.522562+00:00 app[worker.1]: 14:43:14 Moving job to 'failed' queue

我一直在阅读Flask: Passing around background worker job (rq, redis),这听起来像是一个类似的问题,但我不清楚如何解决这个问题以允许我访问存储在会话中的数据目的

4

1 回答 1

1

在为此苦苦挣扎了一段时间后,我得出的结论是,可能无法将对象传递到 RQ 。我最终将参数转换为 json 字符串并以这种方式传递它们。

args= json.dumps({"posting": session['posting'], "folder": session['folder']})

然后你可以运行:

 foreground_vs_background(myfunction, args):
于 2018-03-11T13:11:23.540 回答