-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add async support for temporary file handling. Closes #344 #867
Merged
Merged
Changes from 11 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
beb8285
feat: Add async support for temporary file handling. Closes #344
11kkw 07730ef
Merge branch 'master' into async-tempfile-support
agronholm 93c9ae4
Fix: Apply requested changes from @agronholm
11kkw 546a462
Merge branch 'master' into async-tempfile-support
agronholm 684176b
fix: add blank lines after control blocks, fix Pyright errors, and ig…
11kkw 2c89b61
fix(tempfile): apply review feedback and improve async tempfile
11kkw 2e6ea50
Update src/anyio/_core/_tempfile.py
11kkw db3c2cd
Update src/anyio/_core/_tempfile.py
11kkw ad12c85
tempfile: update type annotations, fix Py3.14 compatibility, and upda…
11kkw 50c498f
Merge branch 'master' into async-tempfile-support
agronholm b7a9722
Removed redundant py3.14 fixes
agronholm 2a1a1cf
Update src/anyio/_core/_tempfile.py
11kkw e608cdc
Update src/anyio/_core/_tempfile.py
11kkw 4cdadc6
Update tests/test_tempfile.py
11kkw bd36d3c
Update tests/test_tempfile.py
11kkw 86719ec
Update tests/test_tempfile.py
11kkw c129057
Update tests/test_tempfile.py
11kkw 6551774
Update tests/test_tempfile.py
11kkw f730c72
Update tests/test_tempfile.py
11kkw 4dc6c71
Update tests/test_tempfile.py
11kkw bd61572
Update tests/test_tempfile.py
11kkw 1509c28
docs: Include TemporaryFile-related classes in API reference
11kkw 78bb1d1
api.rst update
11kkw 9eaa5c5
Merge branch 'master' into async-tempfile-support
agronholm 77e7259
Reimplemented SpooledTemporaryFile and improved type annotations
agronholm 058ab8a
Updated tests
agronholm e88499e
Reduced the number of mypy errors
agronholm e57dd1d
Silenced the remaining mypy errors
agronholm 8b59b66
Merge branch 'master' into async-tempfile-support
agronholm 7d8f4b7
Added some finishing touches
agronholm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ The manual | |
subprocesses | ||
subinterpreters | ||
fileio | ||
tempfile | ||
signals | ||
testing | ||
api | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
Asynchronous Temporary File and Directory | ||
========================================= | ||
|
||
.. py:currentmodule:: anyio | ||
|
||
This module provides asynchronous wrappers for handling temporary files and directories | ||
using the :mod:`tempfile` module. The asynchronous methods execute blocking operations in worker threads. | ||
|
||
Temporary File | ||
-------------- | ||
|
||
:class:`TemporaryFile` creates a temporary file that is automatically deleted upon closure. | ||
|
||
**Example:** | ||
|
||
.. code-block:: python | ||
|
||
from anyio import TemporaryFile, run | ||
|
||
async def main(): | ||
async with TemporaryFile(mode="w+") as f: | ||
await f.write("Temporary file content") | ||
await f.seek(0) | ||
print(await f.read()) # Output: Temporary file content | ||
|
||
run(main) | ||
|
||
Named Temporary File | ||
-------------------- | ||
|
||
:class:`NamedTemporaryFile` works similarly to :class:`TemporaryFile`, but the file has a visible name in the filesystem. | ||
|
||
**Example:** | ||
|
||
.. code-block:: python | ||
|
||
from anyio import NamedTemporaryFile, run | ||
|
||
async def main(): | ||
async with NamedTemporaryFile(mode="w+", delete=True) as f: | ||
print(f"Temporary file name: {f.name}") | ||
await f.write("Named temp file content") | ||
await f.seek(0) | ||
print(await f.read()) | ||
|
||
run(main) | ||
|
||
Spooled Temporary File | ||
---------------------- | ||
|
||
:class:`SpooledTemporaryFile` is useful when temporary data is small and should be kept in memory rather than written to disk. | ||
|
||
**Example:** | ||
|
||
.. code-block:: python | ||
|
||
from anyio import SpooledTemporaryFile, run | ||
|
||
async def main(): | ||
async with SpooledTemporaryFile(max_size=1024, mode="w+") as f: | ||
await f.write("Spooled temp file content") | ||
await f.seek(0) | ||
print(await f.read()) | ||
|
||
run(main) | ||
|
||
Temporary Directory | ||
------------------- | ||
|
||
The :class:`TemporaryDirectory` provides an asynchronous way to create temporary directories. | ||
|
||
**Example:** | ||
|
||
.. code-block:: python | ||
|
||
from anyio import TemporaryDirectory, run | ||
|
||
async def main(): | ||
async with TemporaryDirectory() as temp_dir: | ||
print(f"Temporary directory path: {temp_dir}") | ||
|
||
run(main) | ||
|
||
Low-Level Temporary File and Directory Creation | ||
----------------------------------------------- | ||
|
||
For more control, the module provides lower-level functions: | ||
|
||
- :func:`mkstemp` - Creates a temporary file and returns a tuple of file descriptor and path. | ||
- :func:`mkdtemp` - Creates a temporary directory and returns the directory path. | ||
- :func:`gettempdir` - Returns the path of the default temporary directory. | ||
- :func:`gettempdirb` - Returns the path of the default temporary directory in bytes. | ||
|
||
**Example:** | ||
|
||
.. code-block:: python | ||
|
||
from anyio import mkstemp, mkdtemp, gettempdir, run | ||
import os | ||
|
||
async def main(): | ||
fd, path = await mkstemp(suffix=".txt", prefix="mkstemp_", text=True) | ||
print(f"Created temp file: {path}") | ||
|
||
temp_dir = await mkdtemp(prefix="mkdtemp_") | ||
print(f"Created temp dir: {temp_dir}") | ||
|
||
print(f"Default temp dir: {await gettempdir()}") | ||
|
||
os.remove(path) | ||
|
||
run(main) | ||
|
||
.. note:: | ||
Using these functions requires manual cleanup of the created files and directories. | ||
|
||
.. seealso:: | ||
|
||
- Python Standard Library: :mod:`tempfile` (`official documentation <https://docs.python.org/3/library/tempfile.html>`_) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which open call is blocking?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The one in
test_mkstemp()
.