![IB python api套接字WinError 10038](https://linux22.com/image/1598308/IB%20python%20api%E5%A5%97%E6%8E%A5%E5%AD%97WinError%2010038.png)
这在 Interactive Brokers 的 API 中被编码为“connection.py”。运行时,它会返回错误“尝试对非套接字执行操作”,位于“buf = self.socket.recv(4096)”。我尝试将传递给 recv 的值更改为 7497,以匹配套接字端口,但收到相同的消息。我认为这可能是格式错误,但我对 python 还不太熟悉,无法确定。
logger = logging.getLogger(__name__)
class Connection:
def __init__(self, host, port):
self.host = host
self.port = port
self.socket = None
self.wrapper = None
self.lock = threading.Lock()
def connect(self):
try:
self.socket = socket.socket()
#TODO: list the exceptions you want to catch
except socket.error:
if self.wrapper:
self.wrapper.error(NO_VALID_ID, FAIL_CREATE_SOCK.code(), FAIL_CREATE_SOCK.msg())
try:
self.socket.connect((self.host, self.port))
except socket.error:
if self.wrapper:
self.wrapper.error(NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg())
self.socket.settimeout(1) #non-blocking
def disconnect(self):
self.lock.acquire()
try:
if self.socket is not None:
logger.debug("disconnecting")
self.socket.close()
self.socket = None
logger.debug("disconnected")
if self.wrapper:
self.wrapper.connectionClosed()
finally:
self.lock.release()
def isConnected(self):
return self.socket is not None
def sendMsg(self, msg):
logger.debug("acquiring lock")
self.lock.acquire()
logger.debug("acquired lock")
if not self.isConnected():
logger.debug("sendMsg attempted while not connected, releasing lock")
self.lock.release()
return 0
try:
nSent = self.socket.send(msg)
except socket.error:
logger.debug("exception from sendMsg %s", sys.exc_info())
raise
finally:
logger.debug("releasing lock")
self.lock.release()
logger.debug("release lock")
logger.debug("sendMsg: sent: %d", nSent)
return nSent
def _recvMsg(self):
if not self.isConnected():
logger.debug("recvMsg attempted while not connected, releasing lock")
return b""
try:
buf = self.recvAllMsg()
# receiving 0 bytes outside a timeout means the connection is either
# closed or broken
if len(buf) == 0:
logger.debug("socket either closed or broken, disconnecting")
self.disconnect()
except socket.timeout:
logger.debug("socket timeout from recvMsg %s", sys.exc_info())
buf = b""
else:
pass
return buf
def recvAllMsg(self):
cont = True
allbuf = b""
while cont and self.socket is not None:
buf = self.socket.recv(4096)
allbuf += buf
logger.debug("len %d raw:%s|", len(buf), buf)
if len(buf) < 4096:
cont = False
return allbuf
答案1
4096 是缓冲区大小,而不是端口号。发生此错误是因为一个线程退出而另一个线程正在尝试写入(写入不存在的位置)。这是一个 IB 问题,但不会造成任何问题,只是有点丑陋。
答案2
在您的处实施的解决方法EClient/EWrapper 类:
- 创建套接字关闭函数
- 在调用断开连接方法之前使用它
# create function
def _socketShutdown(self):
self.conn.lock.acquire()
try:
if self.conn.socket is not None:
self.conn.socket.shutdown(socket.SHUT_WR)
finally:
self.conn.lock.release()
# use it
self._socketShutdown()
time.sleep(1)
self.disconnect()