Python Schedule模块仅在第一次使用

问题描述 投票:1回答:1

我目前在一家金融公司担任定量实习生。我想做的是让系统每天16:30将今天的股票市场数据更新为.csv。

我在网上搜索并找到了Schedule模块,看起来很简单,所以我实现了它。在第一天(星期一),它做得很好,并相应更新。但是,今天我检查服务器昨天(星期二)的日期没有更新,并报告错误。

我如何编写我的代码如下:

def job():
    stockss = list(all_instruments(type='CS').order_book_id.values)
    for stock in stockss:
    d = datetime.datetime.today().strftime('%Y-%m-%d')
    a = rq.get_price(stock,str(d),str(d))
    df = pd.DataFrame(a)
    with open(str(stock)+'.csv','a') as f:
        df.to_csv(f, header = False)

schedule.every().monday.at("16:30").do(job)
schedule.every().tuesday.at("16:30").do(job)
schedule.every().wednesday.at("16:30").do(job)
schedule.every().thursday.at("16:30").do(job)
schedule.every().friday.at("16:30").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

谢谢!

python scheduled-tasks scheduler finance
1个回答
0
投票

你可以使用另一个更强大的模块apscheduler,这是一个例子:

# ... your import
from apscheduler.schedulers.blocking import BlockingScheduler


def job():
    stockss = list(all_instruments(type='CS').order_book_id.values)
    for stock in stockss:
    d = datetime.datetime.today().strftime('%Y-%m-%d')
    a = rq.get_price(stock,str(d),str(d))
    df = pd.DataFrame(a)
    with open(str(stock)+'.csv','a') as f:
        df.to_csv(f, header = False)


if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_job(job, 'cron', day_of_week="mon-fri", hour="16", minute="30")  # run on Monday to Friday at 16:30
    print('Press Ctrl+C to exit')

    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        pass

这是相关的文档:

https://apscheduler.readthedocs.io/en/latest/modules/triggers/cron.html#module-apscheduler.triggers.cron

https://apscheduler.readthedocs.io/en/latest/index.html

© www.soinside.com 2019 - 2024. All rights reserved.