При скачивании файла образуются новые потоки, которые не закрываются
Всё действо происходит в локальной сети.
Есть код сервера написанный на Sanic, который получает файлы или отдает файлы по запросу в json. Работает на raspbian
Вот:
from sanic import Sanic
from sanic import response
import asyncio
import traceback
import time
import os
PATH_APP = os.path.abspath(os.path.dirname(__file__))
PORT = 8001
PATH_IMAGES = PATH_APP
PATH_FILES = PATH_APP
print(PATH_APP)
app = Sanic("localChatFileServer")
@app.route("/download/images/",methods = ["GET"])
async def get_image(request):
content = request.json
if content["request"] == "get_image":
files = os.listdir(PATH_IMAGES)
if content["name_image"] in files:
temp_image_path = os.path.join(PATH_IMAGES,content["name_image"])
return await response.file(temp_image_path)
else:
return response.json({"request":"false","server_message":"file not exists"})
@app.route("/download/files",methods = ["GET"])
async def get_file(request):
content = request.json
if content["request"] == "get_file":
files = os.listdir(PATH_FILES)
if content["name_file"] in files:
temp_file_path = os.path.join(PATH_FILES,content["name_file"])
return await response.file(temp_file_path)
else:
return response.json({"request":"false","server_message":"file not exists"})
@app.route("/upload/images/",methods = ["POST"])
async def post_image(request):
try:
path_file = os.path.join(PATH_IMAGES,request.files["image_bytes"][0].name)
with open(path_file,"wb") as f:
f.write(request.files["image_bytes"][0].body)
return response.json({"request":"OK","server_message":"image is uploaded"})
except:
print(traceback.format_exc(limit=30))
return response.json({"request":"false","server_message":"image is not uploaded"})
@app.route("/upload/files",methods = ["POST"])
async def post_file(request):
try:
path_file = os.path.join(PATH_FILES,request.files["file_bytes"][0].name)
with open(path_file,"wb") as f:
f.write(request.files["file_bytes"][0].body)
return response.json({"request":"OK","server_message":"file is uploaded"})
except:
print(traceback.format_exc(limit=30))
return response.json({"request":"false","server_message":"file is not uploaded"})
async def server_error_handler(request,exception):
return response.json({"request":"false","server_message":"server error - status=500","error":traceback.format_exc(limit=30)})
if __name__ == "__main__":
app.error_handler.add(Exception,server_error_handler)
app.run(host="0.0.0.0",port=PORT)
И код клиента, который получает или отдает файлы. Работает на windows 10
Вот
import aiohttp
import asyncio
import os
IMG_DIR = os.path.abspath(os.path.dirname(__file__))
list_images = ["3.jpg","3.png","5.jpg"]
async def get_image():
for nameimage in list_images:
async with aiohttp.ClientSession() as session:
async with session.get("http://localhost:8001/download/images/",json = {"request":"get_image","name_image":nameimage}) as resp:
image_bytes = await resp.read()
with open(nameimage,"wb") as f:
f.write(image_bytes)
asyncio.run(get_image())
А также есть вот такая картинка до попытки скачать файлы с сервера:
Столько то потоков

Когда передаешь файлы на сервер - все отлично, но если файлы с сервера передаются на клиент, то выплывает такая картина:

А здесь уже столько потоков. Это так и должно быть? Каждый раз при скачивании файла с сервера добавляются новые потоки, я не знаю, может есть все таки предел, я сто файлов не скачивал. Может я что-то не правильно делаю?