Skip to content

Commit

Permalink
self review 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Sep 12, 2024
1 parent cd8de97 commit 2e574f4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/src/django-settings.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
!!! Note

The `ServeStaticMiddleware` class takes all the same configuration options as the `ServeStatic` base class, but rather than accepting keyword arguments to its constructor it uses Django settings. The setting names are just the keyword arguments upper-cased with a `SERVESTATIC_` prefix.
The `ServeStaticMiddleware` class can take the same configuration options as the `ServeStatic` base class, but rather than accepting keyword arguments to its constructor it uses Django settings. The setting names are just the keyword arguments upper-cased with a `SERVESTATIC_` prefix.

---

Expand Down
14 changes: 5 additions & 9 deletions src/servestatic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ def __init__(
opener,
)
self.loop: asyncio.AbstractEventLoop | None = None
self.executor: ThreadPoolExecutor | None = None
self.executor = ThreadPoolExecutor(
max_workers=1, thread_name_prefix="ServeStatic-AsyncFile"
)
self.lock = threading.Lock()
self.file_obj: None | IOBase = None
self.closed = False
Expand All @@ -133,17 +135,12 @@ async def _execute(self, func, *args):
"""Run a function in a dedicated thread, specific to this instance."""
if self.loop is None:
self.loop = asyncio.get_event_loop()
if self.executor is None:
self.executor = ThreadPoolExecutor(
max_workers=1, thread_name_prefix="ServeStatic-AsyncFile"
)
with self.lock:
return await self.loop.run_in_executor(self.executor, func, *args)

def open_raw(self):
"""Open the file without using the executor."""
if self.executor:
self.executor.shutdown(wait=True)
self.executor.shutdown(wait=True)
return open(*self.open_args) # pylint: disable=unspecified-encoding

async def close(self):
Expand All @@ -167,8 +164,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()

def __del__(self):
if self.executor:
self.executor.shutdown(wait=True)
self.executor.shutdown(wait=True)


class EmptyAsyncIterator:
Expand Down

0 comments on commit 2e574f4

Please sign in to comment.