]> git.openstreetmap.org Git - nominatim.git/blob - src/nominatim_api/timeout.py
prepare release 5.1.0.post17
[nominatim.git] / src / nominatim_api / timeout.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Helpers for handling of timeouts for request.
9 """
10 from typing import Union, Optional
11 import asyncio
12
13
14 class Timeout:
15     """ A class that provides helper functions to ensure a given timeout
16         is respected. Can only be used from coroutines.
17     """
18     def __init__(self, timeout: Optional[Union[int, float]]) -> None:
19         self.abs = None if timeout is None else asyncio.get_running_loop().time() + timeout
20
21     def is_elapsed(self) -> bool:
22         """ Check if the timeout has already passed.
23         """
24         return (self.abs is not None) and (asyncio.get_running_loop().time() >= self.abs)