python - 实现Asyncio通过socket同时发送多个文件
问题描述
刚开始使用 asyncio 不确定我有多接近每次运行此功能时,如果它仍在发送,我不想打扰以前的文件循环发送,因此最终我可以使用相同的文件同时发送无限数量的文件功能
async def fileread(self):
loop = asyncio.get_event_loop()
executor = ThreadPoolExecutor(8)
tasks = [loop.run_in_executor(executor,self.fileread())]
#tasks.append(self.fileread())
global flsize
progress = tqdm.tqdm(range(flsize), f"Sending {fp}", unit="B", unit_scale=True,
unit_divisor=1024)
with open(fp, "rb") as f:
bytes_read = f.read(flsize) # NEED TO SEND SMALL BYTS
bytes_read_small = f.read(4096)
while not flsize == 0:
if not bytes_read:
# file transmitting is done
break
global file_sent
file_sent = 1
self.s.sendall(bytes_read) #loop.sendall
progress.update(len(bytes_read))
file_sent = 1
self.s.shutdown()
self.s.close()
await asyncio.wait(tasks)
def connect_to_server(self, *args, **kwargs):
try:
loop = asyncio.new_event_loop()
loop.run_until_complete(self.fileread())
#asyncio.run(self.fileread())
# loop.close() #not sure
except:
print("no op")
解决方案
推荐阅读
- c# - 如何为 VST 插件(如 VST.NET 2 主机中的 vsthost 配置)设置采样率和缓冲区大小
- python - 使用python和正则表达式去除单词中的重复字符串
- java - 在启用 TLS 的情况下使用 kafka-topics cli 命令时出现问题
- mysql - 如何在一行中获得不同的值以及它们各自的平均值作为 mySQL 中的输出
- javascript - if else 条件语句在反应
- sql - 有没有办法从聚合 sql 查询中获取嵌套表?
- swift - 如何为 UICollectionViewCompositionalLayout 中的不同部分设置多个背景视图
- visual-studio-code - VSCode“显示修复”的语言与文本编辑器本身不同
- mysql - 在 MySQL 中检测返回的空集,然后打印一条消息
- c++ - C ++忽略字符串中的\ x,如何使其成为纯文本?