From 3e9ada283c3afca593152139d62962a74a5b6ad9 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sat, 17 Mar 2018 23:41:46 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E6=B7=BB=E5=8A=A0=E7=81=AB=E5=B8=81API?= =?UTF-8?q?=E7=9A=84=E5=90=8C=E6=AD=A5=E6=A8=A1=E5=BC=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/huobi/testtd.py | 8 ++++---- vnpy/api/huobi/vnhuobi.py | 30 ++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/vnpy/api/huobi/testtd.py b/vnpy/api/huobi/testtd.py index b82bcda5..d7061863 100644 --- a/vnpy/api/huobi/testtd.py +++ b/vnpy/api/huobi/testtd.py @@ -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 = '' diff --git a/vnpy/api/huobi/vnhuobi.py b/vnpy/api/huobi/vnhuobi.py index 0246ded9..b838eefe 100644 --- a/vnpy/api/huobi/vnhuobi.py +++ b/vnpy/api/huobi/vnhuobi.py @@ -63,6 +63,9 @@ class TradeApi(object): """交易API""" HUOBI = 'huobi' HADAX = 'hadax' + + SYNC_MODE = 'sync' + ASYNC_MODE = 'async' #---------------------------------------------------------------------- def __init__(self): @@ -70,13 +73,14 @@ class TradeApi(object): 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): @@ -170,11 +179,16 @@ 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):