简体   繁体   English

python 每 2 分钟运行一次脚本,但失败

[英]python run script every 2 mins, but failed

import pandas as pd 
import numpy as np
import datetime
import schedule
import time

ticks = api.ticks(api.Contracts.Stocks["2330"], "2022-08-09")
df = pd.DataFrame({**ticks})
df.ts = pd.to_datetime(df.ts)
df = df[df.volume>200]
df

Above code, works fine.上面的代码,工作正常。 I got data.我得到了数据。

Below code, not working.下面的代码,不起作用。 I got nothing.我什么都没有。 It just keep running but no data coming.它只是继续运行,但没有数据到来。

My goal is to run the code (receive data), every 2 mins automatically.我的目标是每 2 分钟自动运行一次代码(接收数据)。

I counldnt figure out where go wrong.我无法弄清楚 go 哪里错了。
I would need some help.我需要一些帮助。 tried many times and spent a lot of time.试了很多次,也花了很多时间。

import pandas as pd 
import numpy as np
import datetime
import schedule
import time

def show_datafr():
 ticks = api.ticks(api.Contracts.Stocks["2330"], "2022-08-09")
 df = pd.DataFrame({**ticks})
 df.ts = pd.to_datetime(df.ts)
 df = df[df.volume>200]
 df
 
schedule.every(4).seconds.do(show_datafr)

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

if you want to run every 2 min, the schedule line is quite strange.如果你想每 2 分钟运行一次,那么时间表就很奇怪了。 it should be: schedule.every(2).minutes.do(show_datafr)它应该是: schedule.every(2).minutes.do(show_datafr)

instead of:代替:

schedule.every(4).seconds.do(show_datafr) schedule.every(4).seconds.do(show_datafr)

as what you wrote is to run every 4 sec, and possibly the operation cannot be finished in 4 sec, causing it no output因为你写的是每 4 秒运行一次,并且可能无法在 4 秒内完成操作,导致它没有 output

To display df you can import display from IPython.display要显示df你可以从IPython.display导入display

You might want to install it with pip install ipython incase you don't have it installed.您可能希望使用pip install ipython安装它,以防您没有安装它。

import pandas as pd 
import numpy as np
import datetime
from schedule
import time
from IPython.display import display     # Additional import


def show_datafr():
    ticks = api.ticks(api.Contracts.Stocks["2330"], "2022-08-09")
    df = pd.DataFrame({**ticks})
    df.ts = pd.to_datetime(df.ts)
    df = df[df.volume>200]

    display(df)  # To display dataframe


schedule.every(2).minutes.do(show_datafr)  # Remember you said every 2 minutes
    
while True:
    schedule.run_pending()
    time.sleep(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM