【问题标题】:Task scheduling function in python and using the sched library in pythonpython中的任务调度函数以及python中sched库的使用
【发布时间】:2020-07-17 22:48:04
【问题描述】:

我在python中创建了一个任务调度函数,使用sched库并使用sch.enterabs()函数调度了几个函数,它执行几个函数在每个函数都有一定的时间,但是如果第一个函数执行的时间很长,下一个函数将等待第一个函数完成,并且不会在预定的时间工作。我希望它在时机成熟时工作,不管第一个完成,有人帮助我吗?

时间表代码

def schedule_with_properties():
    sch = sched.scheduler(time.time, time.sleep)


    hh = "09"
    mm = "10"
    ss = 52
    ninute = mm - 1


    t1 = (2020, 4, 1, int(hh), int(ninute), ss, 1, 48, 0)
    t2 = (2020, 4, 1, 9, 15, ss, 1, 48, 0)
    t2 = (2020, 4, 1, 9, 20, 0, 1, 48, 0)
      
    time_sec = time.mktime(t1) 
    time_sec2 = time.mktime(t2) 
    time_sec3 = time.mktime(t3) 

    sch.enterabs(time_sec, 0, buyDigital, ())
    sch.enterabs(time_sec2, 1, buyDigital, ())
    sch.enterabs(time_sec3, 2, buyDigital, ())
    sch.run()

【问题讨论】:

  • 为什么不用芹菜?
  • python 解释器在使用像 sched 这样的模块时不会执行抢占式多任务处理。您必须按照@ShreeRangaRaju 的建议使用celery 之类的东西,或者使用async、multiprocessing 或threading 自行处理。
  • 感谢您的回答,继续使用“sched”库并按照建议使用线程,这样,当时机成熟时,它会为该事件创建一个新线程,因此不会干扰任何其他事件,谢谢
  • @ShreeRangaRaju selery 对我打算做的事情没有多大帮助,我只是想要一个小代码 sn-p

标签: python python-3.x scheduled-tasks job-scheduling


【解决方案1】:

感谢您的支持,我一直在使用sched 库并按照建议使用threading,这样,当时机成熟时,它会为该事件创建一个新的thread,因此不会有任何事件干扰其他任何事件,那样

job = []

file= open('list.txt', 'r')

for line in arquivo:
    Ent = 10
    time = "10:00"
    Par = line[3:9]
    Dur = line[1:2]
    Dir = line[19:22]
    x = threading.Thread(target=run, args=(Ent,Par,Dir,Dur,time))
    job.append(x)

for j in job:
        j.start()

运行方法

def run(Ent,Par,Dir,Dur,time):
        sch.enterabs(Hora, 1, someMethod, (Ent,Par,Dir,Dur,time))
    sch.run()



def someMethod(Ent,Par,Dir,Dur,time):
   ......

【讨论】:

    猜你喜欢
    • 2011-06-01
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 2013-03-07
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多