python - 调度和处理在给定日期运行的耗时函数
问题描述
我有一个 python 函数,它应该运行给定的日期和时间,它从 url 中获取视频,将其转换为给定的格式并将其上传到存储中。
此计划函数同步依赖于其他 3 个函数(具有关注点分离)
Def getVideo(url):
#1. download the video from an URL
Return scraped_video
Def convertVideo(scraped_video):
#2. Convert the video to a given format
Return file_ouput_path
Def sendVideo(file):
#3. Upload the video to a given Gdrive or Dropbox
GrabAndConvertThenSend(url, notification_email):
Try:
Temp_video = getVideo(url)
file_ouput_path = convertVideo(Temp_video)
sendVideo(file_output_path)
# send notification email with the link
# Schedule the next run
Except Exception as e:
Print(e)
通过 APScheduler 处理函数 GrabAndConvertThenSend() 以在给定日期运行
1.如何实现重试?
有时,由于网络问题或 API 可用性,主要功能可能会中止。 例如:该功能已经下载了视频,但未能将其上传到存储中。
如何在不重新下载视频的情况下恢复停止的位置?我坚持将状态(下载、转换、上传)存储在数据库中,这是正确的方法吗?
2.像这样链接功能是否正确?或者我应该依赖 events/listeners ,甚至是排队作业(当任务 A 完成时,它会将任务 B 排队),但是如何实现这一点,因为我的函数有一个单独的问题
解决方案
如何实现重试?
这是一个通用的设计理念。在这里您可以使用恢复点。它包括设计一个工作流程,其中一个步骤的输入仅在该步骤产生其全部输出后才被删除。如果稍后处理中断,您可以在最后一个成功步骤后重新启动作业。由于关注点分离,它不应该在当前函数中实现,而是在负责以下方面的管理器中实现:
- 在最后一个成功步骤后重新启动
- 成功后调用下一步
在您的伪代码中,此管理器功能应在以下位置实现GrabAndConvertThenSend
:
Def GrabAndConvertThenSend(url, notification_email):
Try:
if not is_a_raw_video_file_present():
Temp_video = getVideo(url)
save_to_(temp_path)
rename(temp_path, raw_video_path)
remove(raw_video_file)
if not is_a_converted_video_file_present():
file_ouput_temp_path = convertVideo(open(raw_video_path).read())
rename(file_output_temp_path, file_output_path)
remove(raw_video_path)
if is_a_converted_video_file_present():
sendVideo(file_output_path)
remove(file_output_path)
# send notification email with the link
# Schedule the next run
Except Exception as e:
Print(e)
# schedule with a shorter time to finish processing
像这样链接功能是否正确?
这是一个品味问题。重要的是特征。上面的伪代码实现了恢复点并将其用于重试。如果它们满足您的要求,您可以使用其他作业管理工具,或者像这样手动使用
推荐阅读
- android - 为什么这些切换在 ListBuilder 中同时工作?
- r - 将相似的元素组合在一起
- php - 如何将数据从 Excel 导入 Laravel 中的 mysql 数据库?(php、laravel、mysql)
- java - 如何使用 open office uno API 克隆 TextTable 并将 N 个克隆的 TextTable 粘贴到原始 TextTable 下方
- django - 在 django 中找不到 GDAL 库。我能做些什么来解决这个问题?
- r - 根据两个类别对每个 id 进行排名
- html - 如何通过一键单击以角度订阅退订?
- flask - 使用 Flask-SQLAlchemy 连接到谷歌云 SQL 错误
- reactjs - 获取选中的下拉项并显示在控制台日志中
- node.js - npm/yarn/yarn2/pnpm 首先获取 package.json 以解析“os”字段吗?