开发环境 windows ,在新的线程中处理数据接收报错: OSError: [WinError 10038] 在一个非套接字上尝试了一个操作。错在__execute__中的 self.request.recv(8192),不使用线程的话没有问题。请教大家问题在哪了
@log("任务已准备开始。。。")
def start(self):
if not self.status["started"] and \
not self.status["finished"]:
self.status["started"] = True
t = threading.Thread(None, self.__execute__)
t.start()
else:
raise TaskAlreadyStartedError("任务已经开启,不能重复开启相同的任务。。。")
@log("正在执行写入任务...")
def __execute__(self):
filename = self.fileInfo.get_name()
full_path = self.backupDirectory + "/" + filename
total_len = 0
with open(full_path, 'wb') as file:
while True:
try:
data = self.request.recv(8192)
if not data:
break
file.write(data)
total_len += len(data)
self.currentProgress = total_len
self.report_progress()
except Exception as error:
if total_len != self.fileInfo.get_size(): # 客户端提前关闭
self.status["finished"] = False
self.status["stopped"] = True
if self.auto_rollback:
file.close() # 释放当前文件句柄,否则造成文件无法回滚
try:
self.rollback()
except Exception as rollbackerror:
logging.error(str(rollbackerror))
logging.error(str(error))
break
if total_len == self.fileInfo.get_size():
self.status["finished"] = True
self.status["stopped"] = True
print("任务%s 已经把文件%s 保存到%s" % (str(self.taskId), filename, self.backupDirectory))