Home > OS >  python run script every 2 mins, but failed
python run script every 2 mins, but failed

Time:08-10

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.

I counldnt figure out where go wrong.
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)

CodePudding user response:

if you want to run every 2 min, the schedule line is quite strange. it should be: schedule.every(2).minutes.do(show_datafr)

instead of:

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

CodePudding user response:

To display df you can import display from IPython.display

You might want to install it with pip install ipython incase you don't have it installed.

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)
  • Related