python - 如何确保 Redis 服务器上的这个 Python RQ 任务正在执行?
问题描述
在 Flask 应用程序中,我有一个函数,它接受一个列表列表并将每个列表作为一行添加到 Excel 文件中。当请求不会超时时,此功能在开发服务器上运行良好。在这里,它被包裹在try
/except
块中。这就是我需要在 prod 服务器上将其作为排队任务调用的方式。
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 服务器上,什么会阻止应用程序的队列执行该作业?
解决方案
推荐阅读
- python - Kivymd 无法将标签和文本字段保持在同一行
- mysql - 缺少数据库时如何迁移 wordpress 网站?
- android - Android 应用程序在 android 10 上崩溃
- swagger - swagger 编辑器 io 的默认格式是什么?
- powershell - 更改windows下哪些用户代码deploy执行脚本
- javascript - 砌体图像在 ajax 调用上未正确定位
- ios - 如何在 SwiftUI 的同一行中创建文本和可点击链接?
- python - 多类分类的学习曲线
- javascript - 如何在纯 JS 中编写同步 AJAX
- python - 'start_p' 参数在 pmd autoarima 中不生效