[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对象并初始化
api = TradeApi() api = TradeApi()
api.init(api.HADAX, accessKey, secretKey) api.init(api.HADAX, accessKey, secretKey, mode=api.SYNC_MODE)
api.start() api.start()
# 查询 # 查询
#api.getSymbols() print api.getSymbols()
#api.getCurrencys() print api.getCurrencys()
#api.getTimestamp() print api.getTimestamp()
accountid = '' accountid = ''

View File

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