How do I loop endlessly without bursting memory?

5

I'm trying to make a program that makes requests on a DDE server, so I need to collect the data every 0.1 sec. But at each iteration of the program the computer's memory increases and in the end to the script.

I'm trying like this:

def requsitaDDE (y, j):    
    global quote

    if j == 4:
        time.sleep(.1)
        #old_quote = quote[0]
        quote = None
        quote = []
        return requsitaDDE(y, 0)

     else:
        current_quote = QUOTE_client.request(y[j])
        quote.append(current_quote)

        requsitaDDE(y, j + 1)

    requsitaDDE(y,0)

But I've done that too:

while 1:
    time.sleep(.1)

    quote = []     

    for i in symbols:
        current_quote = QUOTE_client.request(i)
        quote.insert(y, current_quote)
        y += 1
    print quote

    y = 0

Expensive,

The memory increment problem continues, now I did as suggested by @Marks:

while 1:
gc.collect()
#time.sleep(.1)
mem_usage = memory_usage(-1, interval=.2, timeout=1)
print(mem_usage)
y+= 1
print y

#if y == 20000:
#    break

cur.execute("SELECT MAX(rowid) FROM winj") # use your col
linhaBd = cur.fetchone()
linhaBd[0]

if old_quote != QUOTE_client.request(symbols[0]):

    for i in symbols:

        current_quote = QUOTE_client.request(i)#.split("\t") 

        if symbols.index(i) == 0:
            cur.execute('INSERT INTO winj(rowid, preco) VALUES(?, ?)' , [(linhaBd[0] + 1), current_quote])
            conn.commit()  
            old_quote = current_quote
            print current_quote

        if symbols.index(i) == 1:
            cur.execute('UPDATE winj SET quantidade = ? WHERE rowid = ?' , [current_quote, (linhaBd[0] + 1)])
            conn.commit()

        if symbols.index(i) == 2:
            cur.execute('UPDATE winj SET hora = ? WHERE rowid = ?' , [current_quote, (linhaBd[0] + 1)])
            conn.commit()

        if symbols.index(i) == 3:
            cur.execute('UPDATE winj SET data = ? WHERE rowid = ?' , [current_quote, (linhaBd[0] + 1)])
            conn.commit()     

If anyone has any ideas, thank you.

    
asked by anonymous 23.02.2017 / 20:26

1 answer

4

The memory is increasing because each iteration is adding a value to the variable quote .

A solution is instead of writes this data collected in memory is save to disk. It can be in some file or in a database.

Example with writing to files:

while 1:
    time.sleep(.1)

    for i in symbols:
        current_quote = QUOTE_client.request(i)

        arq = open('/tmp/lista.txt', 'a')
        texto = y + current_quote
        arq.write(texto)
        arq.close()
        y += 1

    y = 0

When you need to consult the collected data, simply access the file.

    
24.02.2017 / 00:07