Skip to content

Commit

Permalink
add multithread support
Browse files Browse the repository at this point in the history
  • Loading branch information
rainx committed Jun 23, 2017
1 parent eedb6ba commit 62c35b6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 19 deletions.
10 changes: 10 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


1.1
------
* 增加多线程支持
* 对disconnect增加了异常捕获

1.0
------
* 初始版本
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ api.get_xdxr_info(1, '600300')
api.get_finance_info(0, '000001')
```

### 多线程支持

由于Python的特性,一般情况下,不太建议使用多线程代码,如果需要并发访问,建议使用多进程来实现,如果要使用多线程版本,请在初始化时设置multithread参数为True

```python
api = TdxHq_API(multithread=True)
```

### 调试模式

Expand Down
8 changes: 7 additions & 1 deletion pytdx/bin/hqget.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
import pickle
from functools import reduce

api = TdxHq_API()

mtstr = os.getenv("TDX_MT", "")
mt = False
if mtstr:
mt = True

api = TdxHq_API(multithread=mt)


def get_security_quotes(params):
Expand Down
41 changes: 25 additions & 16 deletions pytdx/hq.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@
from pytdx.params import TDXParams

from pytdx.parser.setup_commands import SetupCmd1, SetupCmd2, SetupCmd3
import threading

CONNECT_TIMEOUT = 5.000
RECV_HEADER_LEN = 0x10

class TdxHq_API(object):

def __init__(self):
def __init__(self, multithread=False):
self.need_setup = True
if multithread:
self.lock = threading.Lock()
else:
self.lock = None

def connect(self, ip, port):
"""
Expand Down Expand Up @@ -67,8 +72,11 @@ def connect(self, ip, port):
def disconnect(self):
if self.client:
log.debug("disconnecting")
self.client.shutdown(socket.SHUT_RDWR)
self.client.close()
try:
self.client.shutdown(socket.SHUT_RDWR)
self.client.close()
except Exception as e:
log.debug(str(e))
log.debug("disconnected")

def close(self):
Expand All @@ -78,6 +86,7 @@ def close(self):
"""
self.disconnect()


def __enter__(self):
return self

Expand All @@ -92,67 +101,67 @@ def setup(self):
#### API List

def get_security_bars(self, category, market, code, start, count):
cmd = GetSecurityBarsCmd(self.client)
cmd = GetSecurityBarsCmd(self.client, lock=self.lock)
cmd.setParams(category, market, code, start, count)
return cmd.call_api()

def get_index_bars(self, category, market, code, start, count):
cmd = GetIndexBarsCmd(self.client)
cmd = GetIndexBarsCmd(self.client, lock=self.lock)
cmd.setParams(category, market, code, start, count)
return cmd.call_api()

def get_security_quotes(self, all_stock):
cmd = GetSecurityQuotesCmd(self.client)
cmd = GetSecurityQuotesCmd(self.client, lock=self.lock)
cmd.setParams(all_stock)
return cmd.call_api()

def get_security_count(self, market):
cmd = GetSecurityCountCmd(self.client)
cmd = GetSecurityCountCmd(self.client, lock=self.lock)
cmd.setParams(market)
return cmd.call_api()

def get_security_list(self, market, start):
cmd = GetSecurityList(self.client)
cmd = GetSecurityList(self.client, lock=self.lock)
cmd.setParams(market, start)
return cmd.call_api()

def get_minute_time_data(self, market, code):
cmd = GetMinuteTimeData(self.client)
cmd = GetMinuteTimeData(self.client, lock=self.lock)
cmd.setParams(market, code)
return cmd.call_api()

def get_history_minute_time_data(self, market, code, date):
cmd = GetHistoryMinuteTimeData(self.client)
cmd = GetHistoryMinuteTimeData(self.client, lock=self.lock)
cmd.setParams(market, code, date)
return cmd.call_api()

def get_transaction_data(self, market, code, start, count):
cmd = GetTransactionData(self.client)
cmd = GetTransactionData(self.client, lock=self.lock)
cmd.setParams(market, code, start, count)
return cmd.call_api()

def get_history_transaction_data(self, market, code, start, count, date):
cmd = GetHistoryTransactionData(self.client)
cmd = GetHistoryTransactionData(self.client, lock=self.lock)
cmd.setParams(market, code, start, count, date)
return cmd.call_api()

def get_company_info_category(self, market, code):
cmd = GetCompanyInfoCategory(self.client)
cmd = GetCompanyInfoCategory(self.client, lock=self.lock)
cmd.setParams(market, code)
return cmd.call_api()

def get_company_info_content(self, market, code, filename, start, length):
cmd = GetCompanyInfoContent(self.client)
cmd = GetCompanyInfoContent(self.client, lock=self.lock)
cmd.setParams(market, code, filename, start, length)
return cmd.call_api()

def get_xdxr_info(self, market, code):
cmd = GetXdXrInfo(self.client)
cmd = GetXdXrInfo(self.client, lock=self.lock)
cmd.setParams(market, code)
return cmd.call_api()

def get_finance_info(self, market, code):
cmd = GetFinanceInfo(self.client)
cmd = GetFinanceInfo(self.client, lock=self.lock)
cmd.setParams(market, code)
return cmd.call_api()

Expand Down
16 changes: 15 additions & 1 deletion pytdx/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ResponseRecvFails(BaseException):

class BaseParser(object):

def __init__(self, client):
def __init__(self, client, lock=None):
self.client = client
self.data = None
self.send_pkg = None
Expand All @@ -38,6 +38,11 @@ def __init__(self, client):
self.rsp_body = None
self.rsp_header_len = RSP_HEADER_LEN

if lock:
self.lock = lock
else:
self.lock = None

def setParams(self, *args, **xargs):
"""
构建请求
Expand All @@ -51,7 +56,16 @@ def parseResponse(self, body_buf):
def setup(self):
pass


def call_api(self):
if self.lock:
with self.lock:
log.debug("sending thread lock api call")
result = self._call_api()
else:
result = self._call_api()
return result
def _call_api(self):

self.setup()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup, find_packages

setup(name='pytdx',
version='1.0',
version='1.1',
description='A Python Interface to TDX protocol',
author='RainX<Jing Xu>',
author_email='[email protected]',
Expand Down

0 comments on commit 62c35b6

Please sign in to comment.