search

Home  >  Q&A  >  body text

python - 如何定时从服务器下载一个文件?

大家讲道理大家讲道理2912 days ago475

reply all(5)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:01:39

    Method 1: sched module

    # -*- coding:utf-8 -*-
    import time
    import os
    import sched
    
    # 初始化sched模块的scheduler类
    # 第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞。
    schedule = sched.scheduler(time.time, time.sleep)
    
    # 被周期性调度触发的函数
    def executeCommand(cmd, inc):
        os.system(cmd)
        # 循环执行
        schedule.enter(inc, 0, executeCommand, ('echo 又过了5秒钟', inc))
    
    
    def main(cmd, inc=60):
        # enter四个参数分别为:间隔事件、优先级(用于同时间到达的两个事件同时执行时定序)、被调用触发的函数,
        # 给该触发函数的参数(tuple形式)
        schedule.enter(0, 0, executeCommand, (cmd, inc))
        schedule.run()
    
    
    # 每5秒查发一次
    if __name__ == '__main__':
        main('echo 过了5秒钟', 5)
    

    Method 2: APScheduler framework

    > [官方文档][1]
    

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:01:39

    You can use celery, asynchronous task framework, to set up scheduled tasks

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:01:39

    Set a scheduled task

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:01:39

    Is there any ready-made cron scheduled task scheduling under the Linux system (the main reason is that it is easy to learn, takes less time, and is very useful). Write a script to connect to the database, then insert data regularly, and then execute the script regularly. , by the way, there are a lot of crawlers!

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:01:39

    crontab

    reply
    0
  • Cancelreply