[Add]添加火币API的同步模式支持

This commit is contained in:
vn.py 2018-03-17 23:41:46 +08:00
parent b20bf69add
commit 3e9ada283c
2 changed files with 26 additions and 12 deletions

View File

@ -11,13 +11,13 @@ def testTrade():
# 创建API对象并初始化
api = TradeApi()
api.init(api.HADAX, accessKey, secretKey)
api.init(api.HADAX, accessKey, secretKey, mode=api.SYNC_MODE)
api.start()
# 查询
#api.getSymbols()
#api.getCurrencys()
#api.getTimestamp()
print api.getSymbols()
print api.getCurrencys()
print api.getTimestamp()
accountid = ''

View File

@ -64,19 +64,23 @@ class TradeApi(object):
HUOBI = 'huobi'
HADAX = 'hadax'
SYNC_MODE = 'sync'
ASYNC_MODE = 'async'
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
self.accessKey = ''
self.secretKey = ''
self.mode = self.ASYNC_MODE
self.active = False # API工作状态
self.reqid = 0 # 请求编号
self.queue = Queue() # 请求队列
self.pool = None # 线程池
#----------------------------------------------------------------------
def init(self, host, accessKey, secretKey):
def init(self, host, accessKey, secretKey, mode=None):
"""初始化"""
if host == self.HUOBI:
self.hostname = HUOBI_API_HOST
@ -87,10 +91,15 @@ class TradeApi(object):
self.accessKey = accessKey
self.secretKey = secretKey
if mode:
self.mode = mode
#----------------------------------------------------------------------
def start(self, n=10):
"""启动"""
self.active = True
if self.mode == self.ASYNC_MODE:
self.pool = Pool(n)
self.pool.map_async(self.run, range(n))
@ -171,10 +180,15 @@ class TradeApi(object):
#----------------------------------------------------------------------
def addReq(self, path, params, func, callback):
"""添加请求"""
# 异步模式
if self.mode == self.ASYNC_MODE:
self.reqid += 1
req = (path, params, func, callback, self.reqid)
self.queue.put(req)
return self.reqid
# 同步模式
else:
return func(path, params)
#----------------------------------------------------------------------
def processReq(self, req):