Okay, so here is my problem :
I have a function that I call very often (several times per second), and a variable old_date
that is supposed to be computed in the function.
But it seems that this variable is only calculated once, when the function is first called, and then keeps the same value.
Here is the function :
def can_push(date):
"""
Check if the last data was pushed more than TIMEFRAME ago
:param date: string, date of the new data
TIMEFRAME = datetime.timedelta(seconds=1)
DB = MongoClient(HOST, PORT).database
DATE_FMT = '%Y-%m-%dT%H:%M:%S.%fZ'
"""
# If collection doesn't exist, we create it and return True
out = False
# Get MongoDB collection
coll = DB.coll
# Date of the new data (got through websocket)
new_date = datetime.strptime(date, DATE_FMT)
# Date of the last data pushed in DB
cursor = coll.find().sort('{date:-1}').limit(1)
old_date = cursor[0]['date']
print old_date # Format is okay, but date is too old
if (new_date - old_date) > TIMEFRAME:
out = True
# gc.collect()
del old_date
return out
I tried del old_date
but it doesn't seem to work.
After reading this I tried to use gc.collect()
but I'm not very familiar with Python so I'm not sure what it really does (and it doesn't appear to work either).
If I connect to MongoDB shell, I can actually check that the last data pushed in the database is way younger than old_date
shows me.
Does anybody have an idea of another way to forcefully delete old_date
at the end of the function ?
Python version : 2.7.12
Kernel : Ubuntu 16.04