[Add]添加火币API的同步模式支持
This commit is contained in:
parent
b20bf69add
commit
3e9ada283c
@ -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 = ''
|
||||
|
@ -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,12 +91,17 @@ class TradeApi(object):
|
||||
self.accessKey = accessKey
|
||||
self.secretKey = secretKey
|
||||
|
||||
if mode:
|
||||
self.mode = mode
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def start(self, n=10):
|
||||
"""启动"""
|
||||
self.active = True
|
||||
self.pool = Pool(n)
|
||||
self.pool.map_async(self.run, range(n))
|
||||
|
||||
if self.mode == self.ASYNC_MODE:
|
||||
self.pool = Pool(n)
|
||||
self.pool.map_async(self.run, range(n))
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def stop(self):
|
||||
@ -171,10 +180,15 @@ class TradeApi(object):
|
||||
#----------------------------------------------------------------------
|
||||
def addReq(self, path, params, func, callback):
|
||||
"""添加请求"""
|
||||
self.reqid += 1
|
||||
req = (path, params, func, callback, self.reqid)
|
||||
self.queue.put(req)
|
||||
return self.reqid
|
||||
# 异步模式
|
||||
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):
|
||||
|
Loading…
Reference in New Issue
Block a user