簡體   English   中英

如何使用帶有 Redis 隊列的參數將 function 排隊?

[英]How to enqueue a function with an argument with Redis Queue?

I want to enqueue a function to run on background (using Redis Queue) and send a message to the client once the function is done, so I use Flask SocketIO using send(msg) after background task is done. 這里的代碼

 # 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 時,只需將您的 arguments 作為第二個參數

 job = q.enqueue(updateFiles, msg)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM