An Interest In:
Web News this Week
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
- March 14, 2024
wait for a server to respond in python
When starting a python application, our application may wait for a server to respond.
This article introduce a common technique to wait for a server to be available using socket
from python stdlib. A script may wait for a database to start or for an API to become available.
I have written wait_port
, the following function, for the open source library fixtup
. fixtup
starts containers so that a test can run. There is a need to wait before returning control or executing migration code until the container is operational. The wait_port
helper allows you to wait in a hook before returning control to pytest.
# wait postgresql to start until 5 seconds or raise Timeout exceptionwait_port(5432, timeout=5000)
here is the snippet you can reuse in your project
def wait_port(port: int, host: str = 'localhost', timeout: Optional[int] = None, attempt_every: int = 100) -> None: """ wait until a port would be open, for example the port 5432 for postgresql before going further >>> fixtup.helper.wait_port(5432, timeout=5000) :param port: port that has to be open :param remote_ip: host on which the port has to be open. It will be localhost by default :param timeout: timeout in ms before raising TimeoutError. :param attempt_every: time in ms between each attempt to check if the port is responding """ start = monotonic() connected = False while not connected: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: try: s.connect((host, port)) connected = True except ConnectionRefusedError: if timeout is not None and monotonic() - start > (timeout / 1000): raise TimeoutError() sleep(attempt_every / 1000)
We can go further with the principle of this snippet. It can be used to verify that a database is ready for use by checking for the existence of a table or by executing a SELECT 1 + 1
query instead of connecting with a socket.
If you had to write resilient code to wait for a server, share your experience with us in the comments.
Original Link: https://dev.to/farcellier/wait-for-a-server-to-respond-in-python-488e
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To