通过带参数的应用功能按计划运行作业

时间:2019-12-17 09:02:30

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

import schedule
import time

def job(l, order):
    print("I'm working...")
    for i in range(15):
        if i in l[order] :
            print(i)
        else:
            pass

l = [[0,1,2],[3,4,5],[6,7,8],[9,10]]

for idx, val in enumerate(l):
    schedule.every(1).minutes.do(lambda: job(l, idx))

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

我希望输出是这样

I'm working...
0
1
2
I'm working...
3
4
5
I'm working...
6
7
8
I'm working...
9
10

而不是这样:

I'm working...
8
9
10
I'm working...
8
9
10
I'm working...
8
9
10
I'm working...
8
9
10

2 个答案:

答案 0 :(得分:0)

通过您声明的lambda将参数传递给函数时出现问题。 尝试传递这样的参数以进行调度:

import schedule
import time

def job(l, order):
    print("I'm working...")
    for i in range(15):
        if i in l[order] :
            print(i)
        else:
            pass

l = [[0,1,2],[3,4,5],[6,7,8],[9,10]]


for idx in range(0, len(l)):
  schedule.every(1).minutes.do(job, l, idx)
  print("Scheduling for"+str(l[idx]))

while True:
    schedule.run_pending()
    time.sleep(0.1)

答案 1 :(得分:0)

import schedule
import time

def job(val):
    print("I'm working...")
    for i in val:
        print (i)

l = [[0,1,2],[3,4,5],[6,7,8],[9,10]]

for idx, val in enumerate(l):
    schedule.every(1).minutes.do(job, val=val)

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