0

I run a APScheduler alongside Flask application with mongoDB. The goal is to run a function in 30 minutes interval, so I run the job like this:

scheduler_init.add_job(check_for_expire, 'date', run_date=date_activate_until, args=[event],
misfire_grace_time=900)

where "event" is mongoengine object.

The problem is when it goes to function in specified time, the values of "event" are different than the real ones in database (I used debugger to make sure it is so)
Moreover, if I have two different jobs, the values will be different in both two functions and in database (they can be completely different)

Alveona
  • 850
  • 8
  • 17

1 Answers1

0

So, it seems that when you do add_job, the arguments are passed to function by values, not by references. So it copies the "event" object at the time of creating job, therefore I was getting old values

So now in function I do run a query to database to get fresh value:

Event.objects.filter(id = event.id).first()

And make sure your object isn't deleted yet

Alveona
  • 850
  • 8
  • 17
  • Actually, it turned out that the problem is not in references and values (the object is still passed by reference), but in mongoengine queryset My mistake was that I thought the queryset could be dynamic and the new changes will become visible after 30 mins or whatever – Alveona Dec 19 '19 at 09:07