mirror of
https://github.com/Textualize/textual-serve.git
synced 2025-10-17 02:50:37 +03:00
Remove unnecessary lock
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from contextlib import suppress
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
import logging
|
import logging
|
||||||
from typing import AsyncGenerator, TYPE_CHECKING
|
from typing import AsyncGenerator, TYPE_CHECKING
|
||||||
@@ -36,7 +35,6 @@ class DownloadManager:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._active_downloads_lock = asyncio.Lock()
|
|
||||||
self._active_downloads: dict[str, Download] = {}
|
self._active_downloads: dict[str, Download] = {}
|
||||||
"""A dictionary of active downloads.
|
"""A dictionary of active downloads.
|
||||||
|
|
||||||
@@ -66,15 +64,14 @@ class DownloadManager:
|
|||||||
file_name: The name of the file to download.
|
file_name: The name of the file to download.
|
||||||
open_method: The method to open the file with.
|
open_method: The method to open the file with.
|
||||||
"""
|
"""
|
||||||
async with self._active_downloads_lock:
|
self._active_downloads[delivery_key] = Download(
|
||||||
self._active_downloads[delivery_key] = Download(
|
app_service,
|
||||||
app_service,
|
delivery_key,
|
||||||
delivery_key,
|
file_name,
|
||||||
file_name,
|
open_method,
|
||||||
open_method,
|
mime_type,
|
||||||
mime_type,
|
encoding,
|
||||||
encoding,
|
)
|
||||||
)
|
|
||||||
|
|
||||||
async def download(self, delivery_key: str) -> AsyncGenerator[bytes, None]:
|
async def download(self, delivery_key: str) -> AsyncGenerator[bytes, None]:
|
||||||
"""Download a file from the given app service.
|
"""Download a file from the given app service.
|
||||||
@@ -102,12 +99,7 @@ class DownloadManager:
|
|||||||
if not chunk:
|
if not chunk:
|
||||||
# Empty chunk - the app process has finished sending the file.
|
# Empty chunk - the app process has finished sending the file.
|
||||||
incoming_chunks.task_done()
|
incoming_chunks.task_done()
|
||||||
with suppress(asyncio.TimeoutError):
|
del self._active_downloads[delivery_key]
|
||||||
await asyncio.wait_for(
|
|
||||||
download.incoming_chunks.join(), timeout=DOWNLOAD_TIMEOUT
|
|
||||||
)
|
|
||||||
async with self._active_downloads_lock:
|
|
||||||
del self._active_downloads[delivery_key]
|
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
incoming_chunks.task_done()
|
incoming_chunks.task_done()
|
||||||
@@ -129,14 +121,11 @@ class DownloadManager:
|
|||||||
Args:
|
Args:
|
||||||
delivery_key: The delivery key to get the app service for.
|
delivery_key: The delivery key to get the app service for.
|
||||||
"""
|
"""
|
||||||
async with self._active_downloads_lock:
|
for key in self._active_downloads.keys():
|
||||||
for key in self._active_downloads.keys():
|
if key == delivery_key:
|
||||||
if key == delivery_key:
|
return self._active_downloads[key].app_service
|
||||||
return self._active_downloads[key].app_service
|
else:
|
||||||
else:
|
raise ValueError(f"No active download for delivery key {delivery_key!r}")
|
||||||
raise ValueError(
|
|
||||||
f"No active download for delivery key {delivery_key!r}"
|
|
||||||
)
|
|
||||||
|
|
||||||
async def get_download_metadata(self, delivery_key: str) -> Download:
|
async def get_download_metadata(self, delivery_key: str) -> Download:
|
||||||
"""Get the metadata for a download.
|
"""Get the metadata for a download.
|
||||||
@@ -144,5 +133,4 @@ class DownloadManager:
|
|||||||
Args:
|
Args:
|
||||||
delivery_key: The delivery key to get the metadata for.
|
delivery_key: The delivery key to get the metadata for.
|
||||||
"""
|
"""
|
||||||
async with self._active_downloads_lock:
|
return self._active_downloads[delivery_key]
|
||||||
return self._active_downloads[delivery_key]
|
|
||||||
|
|||||||
Reference in New Issue
Block a user