如何确保Redis服务器上的Python RQ任务正在执行?

时间:2020-07-11 14:34:14

标签: python flask redis redis-cli python-rq

在Flask应用程序中,我有一个函数,该函数获取列表列表并将每个列表作为一行添加到Excel文件中。当请求不会超时时,此功能可以在开发服务器上正常工作。在这里,它包装在try / except块中。这就是我需要在生产服务器上将其作为排队任务调用的方式。

def make_excel_file(file_title, lists_to_print):

  try:
    job = get_current_job()

    # returns a list of lists
    # the first list is the header row (titles of attributes)
    # the following lists are all lists of the relevant object attributes
    # as well as some joined object attributes
    # (e.g. user ID, user email, drive year)
    rows = wish_list_rows(lists_to_print)

    wb = Workbook()
    ws = wb.active

    tempfile = NamedTemporaryFile()
    tempfile.name = file_title

    num_rows = len(rows)

    _set_task_progress(0, "excel_progress") #This results in a valid notification
                                            # so I know the function reaches at least this point


    for r in rows:
      ws.append(r)
      wb.save(tempfile)
      tempfile.seek(0)

      _set_task_progress(100.0 * (num_rows/rows.index(r)), "excel_progress") #the number never
                                                                             #updates, so the for loop
                                                                             #must never begin
    stream = tempfile.read()

    response = make_response(stream)
    response.headers['Content-Disposition'] = "attachment; filename={}".format(
      file_title)
    response.mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  except:
    flash("Lists could not be exported to Excel.", "warning")
  finally:
    _set_task_progress(100, "excel_progress")
  if response:
    return response

使用本地服务器的Redis CLI和命令redis-cli monitor,可以看到以下输出:

~$ redis-cli monitor
OK
1594476959.432554 [0 [::1]:61680] "HGETALL" "rq:job:cfabaad5-b586-4aba-90b1-61addb5c9ea9"
1594476959.485371 [0 [::1]:61680] "MULTI"
1594476959.485408 [0 [::1]:61680] "LREM" "rq:queue:spur-hcd-tasks" "1" "cfabaad5-b586-4aba-90b1-61addb5c9ea9"
1594476959.487109 [0 [::1]:61680] "EXEC"
1594476976.799187 [0 [::1]:61680] "MULTI"
1594476976.801143 [0 [::1]:61680] "SADD" "rq:queues" "rq:queue:spur-hcd-tasks"
1594476976.803906 [0 [::1]:61680] "HSET" "rq:job:18be4075-8e3c-409c-a5cf-f82f3d11ba42" "status" "queued"
1594476976.803958 [0 [::1]:61680] "HMSET"
"rq:job:18be4075-8e3c-409c-a5cf-f82f3d11ba42"
"created_at" "2020-07-11T14:16:15.310435Z"
"data" "x\x9c\x94\xbd[\xcc / GIANT BYTE STRING / "
"origin" "name-of-queue"
"description" "app.tasks.make_excel_file(1, file_title='All lists', lists_to_print=[\n            column_1_data, (\"column_2_data\"), column_3_data, column_5_data.\n            , \n..." "enqueued_at" "2020-07-11T14:16:16.749772Z"
"started_at" "" "ended_at" ""
"timeout" "180" "status" "queued"
1594476976.841367 [0 [::1]:61680] "RPUSH" "rq:queue:spur-hcd-tasks" "18be4075-8e3c-409c-a5cf-f82f3d11ba42"
1594476976.841410 [0 [::1]:61680] "EXEC"
1594476977.433481 [0 [::1]:61680] "HGETALL" "rq:job:18be4075-8e3c-409c-a5cf-f82f3d11ba42"

我不确定该如何解释。我还希望帮助您了解如何进一步调试并查看本地Redis服务器上该功能中发生的情况。

编辑:我现在看到配置到应用程序的队列可能有问题。当我使用get_current_job()从RQ获​​得作业时,可以访问它。但是,该应用程序队列的注册表都为空。如果在redis-cli和Python RQ中的Redis服务器上确实存在该应用程序,那么将阻止该应用程序的队列工作?

0 个答案:

没有答案
相关问题