# Path: starlette/concurrency.py
# async def run_in_threadpool(
#     func: typing.Callable[P, T], *args: P.args, **kwargs: P.kwargs
# ) -> T:
#     if kwargs:  # pragma: no cover
#         # run_sync doesn't accept 'kwargs', so bind them in here
#         func = functools.partial(func, **kwargs)
#     return await anyio.to_thread.run_sync(func, *args)
# 
# Path: starlette/types.py



import tempfile
import typing
from collections.abc import Sequence
from shlex import shlex
from urllib.parse import SplitResult, parse_qsl, urlencode, urlsplit
from starlette.concurrency import run_in_threadpool
from starlette.types import Scope

    spool_max_size = 1024 * 1024
    file: typing.BinaryIO
    headers: "Headers"

    def __init__(
        self,
        filename: str,
        file: typing.Optional[typing.BinaryIO] = None,
        content_type: str = "",
        *,
        headers: "typing.Optional[Headers]" = None,
    ) -> None:
        self.filename = filename
        self.content_type = content_type
        if file is None:
            self.file = tempfile.SpooledTemporaryFile(max_size=self.spool_max_size)  # type: ignore  # noqa: E501
        else:
            self.file = file
        self.headers = headers or Headers()

    @property
    def _in_memory(self) -> bool:
        rolled_to_disk = getattr(self.file, "_rolled", True)
        return not rolled_to_disk

    async def write(self, data: bytes) -> None:
        if self._in_memory:
            self.file.write(data)
        else:

# Path: starlette/concurrency.py
# async def run_in_threadpool(
#     func: typing.Callable[P, T], *args: P.args, **kwargs: P.kwargs
# ) -> T:
#     if kwargs:  # pragma: no cover
#         # run_sync doesn't accept 'kwargs', so bind them in here
#         func = functools.partial(func, **kwargs)
#     return await anyio.to_thread.run_sync(func, *args)
# 
# Path: starlette/types.py



# Path: starlette/datastructures.py
import tempfile
import typing
from collections.abc import Sequence
from shlex import shlex
from urllib.parse import SplitResult, parse_qsl, urlencode, urlsplit
from starlette.concurrency import run_in_threadpool
from starlette.types import Scope


    spool_max_size = 1024 * 1024
    file: typing.BinaryIO
    headers: "Headers"

    def __init__(
        self,
        filename: str,
        file: typing.Optional[typing.BinaryIO] = None,
        content_type: str = "",
        *,
        headers: "typing.Optional[Headers]" = None,
    ) -> None:
        self.filename = filename
        self.content_type = content_type
        if file is None:
            self.file = tempfile.SpooledTemporaryFile(max_size=self.spool_max_size)  # type: ignore  # noqa: E501
        else:
            self.file = file
        self.headers = headers or Headers()

    @property
    def _in_memory(self) -> bool:
        rolled_to_disk = getattr(self.file, "_rolled", True)
        return not rolled_to_disk

    async def write(self, data: bytes) -> None:
        if self._in_memory:
            self.file.write(data)
        else:

            await run_in_threadpool(self.file.write, data)