【问题标题】:Scheduling and handling a time consuming function to run at given date调度和处理在给定日期运行的耗时函数
【发布时间】:2021-06-07 06:07:30
【问题描述】:

我有一个 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)

函数 GrabAndConvertThenSend() 通过 APScheduler 处理以在给定日期运行

1.如何实现重试?

有时,由于网络问题或 API 可用性,主要功能可能会中止。 例如:该函数已经下载了视频,但未能将其上传到存储中。

如何在不重新下载视频的情况下从停止的地方恢复? 我坚持将状态(下载,转换,上传)存储在数据库中,这样对吗?

2。像这样链接函数是正确的方法吗?还是我应该依赖事件/侦听器, 甚至排队作业(当任务 A 完成时,它会将任务 B 排队),但是如何实现这一点,因为我的函数有一个单独的关注点

【问题讨论】:

  • 您可以使用celeryflower 进行任务状态监控,并使用redis 或rabbitmq 作为后端和消息代理,或者您可以使用prefect.io

标签: python python-3.x message-queue job-scheduling apscheduler


【解决方案1】:

如何实现重试?

这是一个通用的设计概念。在这里您可以使用恢复点。它包括设计一个工作流程,其中一个步骤的输入仅在该步骤产生其全部输出后才被删除。如果稍后处理中断,您可以在最后一个成功步骤后重新启动作业。由于关注点分离,它不应该在当前函数中实现,而是在一个负责的manager中实现:

  • 在最后一步成功后重新启动
  • 成功后调用下一步

在你的伪代码中,这个管理器功能应该在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

这样链接函数是否正确?

这是一个品味问题。重要的是特征。上面的伪代码实现了恢复点并将其用于重试。如果它们满足您的要求,您可以使用其他作业管理工具,或者像这样使用手动实现

【讨论】:

    猜你喜欢
    • 2016-05-28
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 2011-04-29
    • 1970-01-01
    相关资源
    最近更新 更多