【问题标题】:How to catch the return value of function that is scheduled using python schedule package如何捕获使用 python schedule 包调度的函数的返回值
【发布时间】:2017-06-05 09:02:52
【问题描述】:

我已安排我的函数在每天的特定时间运行。

我将一个参数传递给它,函数返回一个值,我需要在我的代码中使用该值进行进一步计算。

如何捕捉它?

代码sn-p:

import schedule as sd
import time

def dothis(h):
    h-=1
    print("Hour left : "+str(h))
    return(h)

h=24
d="14:00"
sd.every().day.at(d).do(dothis,h) # how to catch the value here?

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

【问题讨论】:

    标签: python schedule


    【解决方案1】:
    import schedule as sd
    import time
    
    def dothis(h):
        h-=1
        print("Hour left : "+str(h))
        return h
    
    h=24
    d="14:00"
    sd.every().day.at(d).do(dothis,h)
    a = dothis(h)
    while True:
        sd.run_pending()
        return_value = a
        time.sleep(1)
    

    只需将函数分配给一个变量,它就会引用该对象

    similar question here answered here

    【讨论】:

      【解决方案2】:

      这对我有用:

      import schedule as sd
      import time
      import pandas as pd
      
      h = pd.Series(24)
      h.to_csv('h.txt',header = None, index = None)
      
      def dothis():
          with open('h.txt', 'r') as f:
              h = int(f.readline())
          print("Hour left : "+str(h))
          h -= 1
          new_h = pd.Series(h)
          new_h.to_csv('h.txt',header = None, index = None)
      
      sd.every(1).seconds.do(dothis) 
      
      
      
      while True:
          sd.run_pending()
          # you can always just call this to get your current value...
          # (even in another sheduled function, which is what i needed)
          """
          with open('h.txt', 'r') as f:
              h = int(f.readline())
          print(h)
          """    
          time.sleep(1)
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多