0

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

Malou
  • 1
  • 1
  • The old_date is coming from the database, who is writing it to the mongo? – geckos Dec 05 '17 at 10:48
  • I am, in an other function. If `can_push` return True, I push my data in the database – Malou Dec 05 '17 at 10:57
  • Multithreading? If so check synchronization – geckos Dec 05 '17 at 10:58
  • I see two possibilities. There is a problem in the writing, or the mongo drive is kind of caching stuff... I doubt that this has to do with memory or something like that – geckos Dec 05 '17 at 11:02
  • Well, I have two websockets and each one is launched with `threading.Thread(...)`, but the data here came from one socket only. – Malou Dec 05 '17 at 11:02
  • But the writing and reading happens in the same thread? – geckos Dec 05 '17 at 11:04
  • The writting in the DB ? I don't think so, data are correctly pushed. What do you mean by "mongo drive is kind of catching stuff" ? – Malou Dec 05 '17 at 11:04
  • Yes, reading and writting for this data are in the same thread – Malou Dec 05 '17 at 11:05
  • Caching, it was a typo – geckos Dec 05 '17 at 11:31
  • can_push is returning true forever? I don't know mongo, is the query okay? I suggest you to stop the can_push function after the query, inspect the result and do another queries. You know pdb? – geckos Dec 05 '17 at 11:35
  • Ok, I will check for MongoDB settings. – Malou Dec 05 '17 at 11:41
  • Yes it always return true, and the query is okay I checked. The debbuger ? Never used it by I go learn right now – Malou Dec 05 '17 at 11:42

1 Answers1

0

Ok so the error was a typo, my bad.

It should be :

cursor = coll.find().sort("date", -1).limit(1)

I was conviced that my request was correct since it worked in MongoDB shell.

Sorry for taking your time and thank you for the help !

Malou
  • 1
  • 1