Skip to navigation

nemo_gym.utils.http_deadline

View as MarkdownOpen in Claude

An HTTP GET that ends at a total time limit.

Module Contents

Classes

NameDescription
_SocketsThe sockets of one request. shutdown ends any read blocked on them, and a socket opened afterwards.
_TrackingAdapterAn adapter whose connections report their sockets, so that a request can be stopped from another thread.

Functions

NameDescription
get_within_deadlineGET url and return its status code and body, raising TimeoutError when timeout_s seconds have passed.

API

class nemo_gym.utils.http_deadline._Sockets()

The sockets of one request. shutdown ends any read blocked on them, and a socket opened afterwards.

_lock
= threading.Lock()
_sockets
list[socket] = []
nemo_gym.utils.http_deadline._Sockets._shutdown_one(
sock: socket.socket
) -> None
staticmethod
nemo_gym.utils.http_deadline._Sockets.add(
sock: socket.socket
) -> None
nemo_gym.utils.http_deadline._Sockets.shutdown() -> None
class nemo_gym.utils.http_deadline._TrackingAdapter(
)

Bases: HTTPAdapter

An adapter whose connections report their sockets, so that a request can be stopped from another thread.

nemo_gym.utils.http_deadline._TrackingAdapter._tracked_pool_classes() -> dict[str, type]
nemo_gym.utils.http_deadline._TrackingAdapter.init_poolmanager(
args: typing.Any = (),
kwargs: typing.Any = {}
) -> None
nemo_gym.utils.http_deadline._TrackingAdapter.proxy_manager_for(
args: typing.Any = (),
kwargs: typing.Any = {}
) -> typing.Any
nemo_gym.utils.http_deadline.get_within_deadline(
url: str,
timeout_s: float,
read_timeout_s: float | None = None,
read_body: bool = True
) -> tuple[int, bytes]

GET url and return its status code and body, raising TimeoutError when timeout_s seconds have passed.

A requests timeout bounds the wait for each read, not the whole call: a peer that keeps sending a byte inside it, in the headers or in the body, keeps the call going. The request therefore runs on a daemon thread that the caller stops waiting for at the limit. At the limit the request’s sockets are shut down, which ends a read that is still waiting, in the headers or in the body, so the thread and its connection are released promptly. With read_body=False the body is not read and b"" is returned: for a readiness check, the answer is the headers.

Exceptions of the request, such as requests.ConnectionError, are raised unchanged.