如何排队与Redis队列参数函数?



我想让一个函数在后台运行(使用Redis Queue),并在函数完成后向客户端发送消息,所以我使用Flask SocketIO在后台任务完成后使用send(msg)。这里的代码

# Server
@app.route("/update")
def update():
job = q.enqueue(updateFiles) # Send function to background
return render_template("index.html")
@socketio.on("message")
def updateFiles(msg): # Function running on background
time.sleep(5)
send(msg) # Sending message to the client after executing functions updateFiles
# HTML (jQuery):
socket.emit("message", "Files updated successfully"); // Emitting message to server
socket.on('message', function (msg) { // Receive message from server and show it on HTML template
$("#message").html(msg);
});

在服务器(Heroku)上有这个错误:

2021-02-22T05:12:00.810166+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/rq/job.py", line 719, in _execute
2021-02-22T05:12:00.810172+00:00 app[worker.1]:     return self.func(*self.args, **self.kwargs)
2021-02-22T05:12:00.810172+00:00 app[worker.1]: TypeError: actualizarExpedientes() missing 1 required positional argument: 'msg'

我看到问题是job = q.enqueue(updateFiles),因为updateFiles需要一个参数。问题是,参数msg来自SocketIO,发送消息与jQuery自动后。如何解决这个问题?

从rq文档

q = Queue('low', connection=redis_conn)
q.enqueue(count_words_at_url,
ttl=30,  # This ttl will be used by RQ
args=('http://nvie.com',),
kwargs={
'description': 'Function description', # This is passed on to count_words_at_url
'ttl': 15  # This is passed on to count_words_at_url function
})

当你调用q.enqueue

时,把你的参数作为第二个参数
job = q.enqueue(updateFiles, msg)

相关内容

  • 没有找到相关文章