如何在Python代码的特定部分使用调度程序?

2024-03-28 10:29:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须运行一个代码来更新关于5个表的详细信息(表名、日期、当天表中的观察数)。这些表格(A、B、C、D、E)的条目数量每天更新。议程是创建一个历史记录表,其中保存所有表的日期记录。我编写了代码,需要应用调度程序,以便代码每24小时运行一次。问题是,我只想把我的部分代码放在调度程序中,而不是完整的代码。我该怎么做

## History table which will only run at the start of the process
data = [('A',len(A),t),('B',len(B),t),('C',len(C),t),('D',len(D),t),('E',len(E),t)]
df = pd.DataFrame(data,columns=['Name','LENGTH','date'])
df

## This code will run everyday and will merge with the history table

import sched
import time

scheduler = sched.scheduler(time.time, time.sleep)


def auto_table():


    import datetime
    t= datetime.date.today()-datetime.timedelta(days=0)
    t

    data = [['A',len(A),t],['B',len(B),t],['C',len(C),t],['D',len(D),t],   ['E',len(E),t]]
    df1 = pd.DataFrame(data,columns=['Name','LENGTH','date'])
    global df
    df=df.merge(df1,how='outer')

    return df



print ('START:', time.time())
scheduler.enter(1, 1, auto_table())

scheduler.run()

我得到了这个错误:

START: 1547819432.8968043

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-199-f7820889e2d5> in <module>()
     22 scheduler.enter(1, 1, auto_table())
     23 
---> 24 scheduler.run()

C:\ProgramData\Anaconda3\lib\sched.py in run(self, blocking)
    152                 delayfunc(time - now)
    153             else:
--> 154                 action(*argument, **kwargs)
    155                 delayfunc(0)   # Let other threads run
    156 

TypeError: 'DataFrame' object is not callable

Tags: therun代码importdataframedfautodata