运行 通过带参数的应用函数按计划进行作业

Run job as schedule by apply function with parameters

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

通过您声明的 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)
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)