TrevP
TrevP

Reputation: 167

python scheduler in a for loop

I have been trying to see if I can use the python schedule module to stagger the ingestion of data into a database by looping through rows in a file and, for example, every 10-second intervals sending the next row of data from the input file into the database. I haven't been able to achieve it yet and so have tried to make the principle work on a simpler problem. I am not an experienced programmer by any means

If I run the code below with t1 commented out I pass a list of numbers through two functions and the code generates the expected result of multiplying each value in the list by two.

import schedule
import time

def the_calc(i):

    j = i * 2
    print(j)
    return j

def gen_newlist(list):

    newer_list = []
    print(list)

    for i in list:

        ## t1
        # res = schedule.every(10).seconds.do(the_calc, i)
        # newer_list.append(res)
        # while True:
        #     schedule.run_pending()
        #     time.sleep(1)

        ## t2
        res = the_calc(i)

        newer_list.append(res)

    return newer_list

if __name__ == '__main__':

    list = [2, 4, 6, 8, 10]

    new_list = gen_newlist(list)
    print(new_list)

If I comment out t2 and uncomment the code at t1 which includes the syntax that I believe is correct for the schedule module, I was hoping each number in the input list would once again be multiplied by 2, but at 10-second intervals.

In fact, the list is processed at 10-second intervals, but it seems as if only the first value in the list is repeatedly picked up and passed to the_calc function resulting in the value being printed out the console, and furthermore not stopping after 5 iterations, which is the length of the list.

It is probably a simple omission or mistake in my code and I am hoping that someone can point me in the right direction.

Upvotes: 0

Views: 1025

Answers (1)

Devansh Soni
Devansh Soni

Reputation: 771

See this line:

schedule.every(10).seconds.do(the_calc, i)

and now let's try to understand what this line is doing.

This line tells the scheduler that the user wants to call function the_calc with a value i every 10 seconds. So, It keeps doing the same thing every 10 seconds and never comes out of the while loop and that's why it never reaches the second value of the list.

I'm not familiar with the schedule module, so I don't know how to do this with this module but you can use time.sleep to do it. Also, you can use threading with this if you don't want to block the main thread. Thanks. Please comment if you have any doubt.

Upvotes: 1

Related Questions