python - 如何防止在 Python 中从数据库中读取的线程累积内存使用量?
问题描述
我有一个代码,它使用线程定期从 mysql 数据库中读取数据并制作该数据的数据框。在较短的时间间隔内,它使用线程定期使用该数据(在此示例中为简单的打印)。每次读取新数据时,旧数据都变得无关紧要。问题是每次第一个线程(获取数据)运行时,这段代码都会累积内存使用量。怎么能防止呢?
我已经尝试过使用垃圾收集器,但没有成功。
## import libraries
from time import sleep
import mysql.connector as sql
import pandas as pd
import threading
import numpy as np
import gc
## define function to get data from mysql database and make pandas dataframe
def get_HLC():
db=sql.connect(host='localhost',user='root',password='',database='algo')
df=pd.read_sql('select *from ticks',con=db,parse_dates=True)
df=pd.DataFrame(df)
df=df.set_index(['timestamp'])
df.index = pd.to_datetime(df.index.astype(np.int64), unit='ms')
df2 = df.resample('60s', how={'last_price': 'ohlc'})
df3 = df.resample('60s', how={'volume': 'sum'})
df3.columns = pd.MultiIndex.from_tuples([('volume', 'sum')])
df4 = pd.concat([df2, df3], axis=1)
df4.iloc[:,3] = df4.iloc[:,3].fillna(method='ffill')
df4.iloc[:,0] = df4.iloc[:,0].fillna(value=df4.iloc[:,3])
df4.iloc[:,1] = df4.iloc[:,1].fillna(value=df4.iloc[:,3])
df4.iloc[:,2] = df4.iloc[:,2].fillna(value=df4.iloc[:,3])
df5 = df4.iloc[-230:,:]
db.close()
del(db, df, df2, df3, df4)
gc.collect()
return df5['last_price']['high'], df5['last_price']['low'], df5['last_price']['close']
## define function that gets the data in a loop
def thread1_Function():
while True:
global low
df5 = get_HLC()
low = df5[1]
del(df5)
gc.collect()
sleep(30)
## start a thread of the get data loop
thread1 = threading.Thread(target=thread1_Function)
thread1.start()
## pause
sleep(5)
## define function that loops and does things with data of thread1
def thread2_Function():
while True:
#do things with thread1 data
print(low)
sleep(15)
## start a thread of the do things with data of thread1 loop
thread2 = threading.Thread(target=thread2_Function)
thread2.start()
解决方案
推荐阅读
- vue.js - Vue-Plyr 加载视频很慢
- windows - TensorFlow 在 Windows 上联合
- ios - 在 Swift 中根据 API 调用设置前景色
- rust - 无法分配给“self.input.cached_token”,因为它是借来的
- bash - 使用换行符或不使用换行符打印字符串序列
- node.js - VueJS 3/路由器/使用推送重定向:未捕获(承诺)类型错误:无法读取未定义的属性(读取“推送”)
- python - 如何覆盖 Outlook 日历事件
- python - ax.add_patch() 中的错误导致坐标缩放不一致/随机
- sparql - RDF4J 的 AST 是否允许 SPARQL 查询重写?
- python - 在数据帧行上应用具有多个操作的函数的有效方法