为什么StreamReader.readexactly()会导致套接字错误,但不会导致StreamReader.read()?
问题描述:
我正在使用Asyncio进行网络编程,在python中编写应用程序。我有类似的代码太:为什么StreamReader.readexactly()会导致套接字错误,但不会导致StreamReader.read()?
try:
data = await self._reader.readexactly(10000)
# Code that uses data
except IncompleteReadError as e:
data = e.parial
# More code
当我尝试运行此代码,它似乎永远不会实际运行。如果我在第二行设置了一个断点,断点将跳闸,但是该函数的其余部分将被忽略。
我得到一个错误最接近的是这起ASYNCIO记录:
Traceback (most recent call last):
File "c:\python36\Lib\asyncio\selector_events.py", line 724, in _read_ready
data = self._sock.recv(self.max_size)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
更换与data = await self._reader.read(10000)
第二线的出现解决了这个问题,但read()
没有解决我的问题,我需要使用。那么为什么会导致套接字错误,但不是read()
?
答
两者之间的唯一区别是,“读”读高达 n个字节,而readexactly读取正是 n个字节,如果到达年底前n个字节是引发IncompleteReadError,这可能会导致您的插座得到你指出的错误。
为什么不能运行except块?该函数仅在该行终止。 – ILikePizza555
,因为您只捕获“IncompleteReadError”,请尝试将其更改为“Exception”,然后查看except块是否得到执行以及抛出的异常类型是什么 – ddor254