tree em
tree em

Reputation: 21721

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 am expecting the output be like this

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

Instead of like this:

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

Upvotes: 1

Views: 493

Answers (2)

user5214530
user5214530

Reputation: 477

It is problem in passing the arguments to your function through the lambda you've stated. Try to pass arguments like this for scheduling:

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)

Upvotes: 2

Shubham Bakshi
Shubham Bakshi

Reputation: 156

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)

Upvotes: 1

Related Questions