简体   繁体   English

Python调度程序不运行工作功能

[英]Python scheduler doesn't run working function

I am trying to schedule a (working) python function load_market_data , but somehow this script doesn't work in scheduler.我正在尝试安排一个(工作)python 函数load_market_data ,但不知何故这个脚本在调度程序中不起作用。

schedule.every(1).day.at("12:46").do(load_market_data)
while True:
    schedule.run_pending()
    time.sleep(60)

I get an error " load_market_data() takes 0 positional arguments but 1 was given "我收到一个错误“ load_market_data() 需要 0 个位置参数,但给出了 1 个

When I add brackets after function当我在函数后添加括号时

schedule.every(1).day.at("12:46").do(load_market_data())
while True:
    schedule.run_pending()
    time.sleep(60)

The function returns the result, but still returns error " the first argument must be callable "函数返回结果,但仍然返回错误“第一个参数必须是可调用的

I found similar issues, but the solution was always making function(self) When I add self load_market_data(self) the function tells me " load_market_data() missing 1 required positional argument: 'self' " As I said before, load_market_data function is working by itself It is pretty long code, so in short it looks like this我发现了类似的问题,但解决方案总是使 function(self) 当我添加 self load_market_data(self) 时,该函数告诉我“ load_market_data() 缺少 1 个必需的位置参数:'self' ” 正如我之前所说,load_market_data 函数正在运行本身是很长的代码,所以简而言之它看起来像这样

def load_market_data():
    if today_value < (lastmonth_min+200):
        print("send email")
    else:
        print("ignore")

PS I was asked to provide a reproducible code, it is not part of question, but if it helps you : PS 我被要求提供可重现的代码,这不是问题的一部分,但如果它对您有帮助:

def load_market_data():

    response = requests.get("https://api.blockchain.info/charts/market-price?timespan=2years&start=2019-07-01&format=csv").text


    response = response.splitlines()

    dfMRKT = pd.DataFrame(response)
    dfMRKT.columns = ['WIP_transAMT']

    dfMRKT['Date'] = pd.DatetimeIndex(dfMRKT['WIP_transAMT'].str.split(',').str[0])  + pd.DateOffset(1)
    pd.to_datetime(dfMRKT['Date'])

    dfMRKT['Mrkt Price USD'] = dfMRKT['WIP_transAMT'].str.split(',').str[1]
    dfMRKT['Mrkt Price USD'] = pd.to_numeric(dfMRKT['Mrkt Price USD'], downcast="float")


    dfMRKT = dfMRKT[['Date','Mrkt Price USD']]
    
    today = date.today()
    last_month = today - pd.DateOffset(months=1)
    half_year = today - pd.DateOffset(month=6)
    dfMRKT_halfyear = dfMRKT[(dfMRKT['Date'] >= half_year)]
    dfMRKT_lastmonth = dfMRKT[(dfMRKT['Date'] >= last_month)]
    lastmonth_min = dfMRKT_lastmonth.min()
    lastmonth_min = lastmonth_min['Mrkt Price USD']
    halfyear_min = dfMRKT_halfyear.min()
    halfyear_min = halfyear_min['Mrkt Price USD']
    today_value = dfMRKT_lastmonth.iloc[[-1]]
    today_value = today_value['Mrkt Price USD']
    today_value = float(today_value)
   
    
    if today_value < (lastmonth_min+200):
        print("send email")
    else:
        print("ignore")

Not sure if this will reach you now 4 months later but I found the solution after having the same problem (I believe).不确定这是否会在 4 个月后到达您的手中,但我在遇到同样的问题后找到了解决方案(我相信)。 Essentially you've been testing your code and submitted it.基本上你一直在测试你的代码并提交它。 Its already running and the "while True" is still running.它已经在运行,而“while True”仍在运行。 When you try to tweak something by giving it the same schedule, it breaks!当您尝试通过给它相同的时间表来调整某些东西时,它会崩溃! You have to open up a fresh instance of the python console and try rerunning it.您必须打开 python 控制台的一个新实例并尝试重新运行它。 That worked for me.那对我有用。 I have no clue why it causes that error, however.但是,我不知道为什么会导致该错误。

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

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