I'm making database cleaning service that deletes data from Postgres tables on regular basis. There are several databases and tables to be cleaned - each of them with their own depth of data to be cleaned and period of cleaning. The process of cleaning is rather similar - 'DELETE ... WHERE timestamp < ...' so it's reasonable to use one method for a list of objects. The task is to schedule regular cleaning jobs with arguments such as table name/object type.
I use schedule
module. It works as follows:
schedule.every().day.at(...time...).do(...)
There has to be Callable object in ...do() - there I pass the name of the method with object_type as an argument. As I have to pass object to Callable then I use lambda:
schedule.every().days.at(...).do(lambda: func(arg))
I do it in a cycle with list of objects. The issue is that when lambda is transferred it is not having been calculated in a moment but is left with argument given. It means that all these lambdas will have the same argument:
for obj in obj_list:
schedule.every().days.at(...).do(lambda: func(obj))
Arg becomes the last one element in obj_list.
The question: is there any way to pass argument to lambda so that it is calculated in the moment of passing?