我有一个函数volumeCounter()
,它位于循环中(在函数looper()
中),每10秒执行一次。它通过api接收数据。之后,它将其存储到sqlite数据库中。当跑了几个小时,公羊会上升。当这个函数有100个或更多线程运行时(每个市场有一个线程,如BTC-ETH),这一点尤为明显。在评论之后,内存使用率没有上升。
我的问题是,这个函数内部会导致内存上升。
def volumeCounter(self):
pass_to_table = api.get_market_history(self.market, 100)["result"] #This is in json format.
for i in pass_to_table:
id_i = i['Id']
time_i = i['TimeStamp'].replace("T", " ")
type_i = i['OrderType']
total = i['Total']
price = i["Price"]
self.cursor.execute(
"""INSERT OR IGNORE INTO {} (id, price, total, type,time) VALUES(?,?,?,?,?)""".format(
self.table_prices), (id_i, price, total, type_i, time_i))
self.connection.commit()
循环looper()
的函数volumeCounter()
。它在开始时在__init__()
执行。
脚本看起来像这样:
key = 'public_key'
secret = 'secret_key'
api = Bittrex(key, secret)
class CryptoScanner:
def __init__(self, market, interval, mailAdrr, mailPass, volume):
# Spremenljivke
self.market = market
self.timeInterval = interval
self.path = "ann{}/{}".format(interval, market)
self.path_2 = self.market.replace('-', '_')
self.database = "{}/{}.db".format(self.path, self.path_2)
self.mailAdrr = mailAdrr
self.mailPass = mailPass
self.sez_length = 0
self.volume = volume
self.scan_mode = False
# Table names
self.table_prices = '{}_prices'.format(self.path_2)
self.table_prices_sum = '{}_prices_sum'.format(self.path_2)
self.table_volume = '{}_volume'.format(self.path_2)
self.table_volume_change = '{}_volume_change'.format(self.path_2)
self.looper()
然后使用多线程为每个市场制作类对象。
for i in api.get_market_summaries()['result']:
if "BTC" in i['MarketName'] and i['BaseVolume'] >= lower_boundary and i['BaseVolume'] <= upper_boundary:
string = i['MarketName']
d["{}".format(string[4:])] = "{}".format(i['MarketName'])
volume["{}".format(string[4:])] = i['BaseVolume']
threads = []
for i in d:
t = threading.Thread(target=CryptoScanner, args=(d[i], interval_sec, mail, password, volume[i]))
threads.append(d)
t.start()
print("Created thread {}".format(d[i]))
添加了bittrex api库详细信息
class Bittrex(object):
"""
Used for requesting Bittrex with API key and API secret
"""
def __init__(self, api_key, api_secret):
self.api_key = str(api_key) if api_key is not None else ''
self.api_secret = str(api_secret) if api_secret is not None else ''
def api_query(self, method, options=None):
"""
Queries Bittrex with given method and options
:param method: Query method for getting info
:type method: str
:param options: Extra options for query
:type options: dict
:return: JSON response from Bittrex
:rtype : dict
"""
if not options:
options = {}
nonce = str(int(time.time() * 1000))
method_set = 'public'
if method in MARKET_SET:
method_set = 'market'
elif method in ACCOUNT_SET:
method_set = 'account'
request_url = (BASE_URL % method_set) + method + '?'
if method_set != 'public':
request_url += 'apikey=' + self.api_key + "&nonce=" + nonce + '&'
request_url += urlencode(options)
return requests.get(
request_url,
headers={"apisign": hmac.new(self.api_secret.encode(), request_url.encode(), hashlib.sha512).hexdigest()}
).json()
def get_market_history(self, market, count):
"""
Used to retrieve the latest trades that have occurred for a
specific market.
/market/getmarkethistory
:param market: String literal for the market (ex: BTC-LTC)
:type market: str
:param count: Number between 1-100 for the number of entries to return (default = 20)
:type count: int
:return: Market history in JSON
:rtype : dict
"""
return self.api_query('getmarkethistory', {'market': market, 'count': count})