Python Requests Timeout: How to Set, Handle, and Optimize It
The Python ecosystem offers many ways to handle networking. Most Python developers rely on the request’s library for its simple design. When making an HTTP request with the python requests library, your code will wait for a response. If a timeout value isn’t specified, it will wait indefinitely. This indicates a ‘wait forever’ condition. This may result in your HTTP request hanging or silently failing if no information ever set back from your server. In Python, you can set a parameter to control how long each request waits for a server to respond. Without it, your Python requests may keep running forever.
Published:
30.10.2025
Reading time:
12 min
This tutorial will walk you through how to implement retry for a session or a call to make sure your Python code is not stuck indefinitely. In modern Python applications, managing timeouts is a vital part of reliable code. Every Python request should include explicit timeout control.
What Are Timeouts in Python Requests?

Inside Python, the timeout logic is part of how the library handles socket-level waits. It helps your Python scripts behave predictably under slow connections. A Python request timeout defines the amount of time a client waits for a response from a server. These timeouts sessions get two different phases, which include a connection phase as well as a read phase. On connectivity, its timeout helps determine how long a connection to a server will get, whereas a read timeout determines how long it will wait after connecting to receive data. Timeout values get a single number or a tuple of two values, depending on your parameter and timing needs. Developers often forget to set timeout values correctly in Python requests, which leads to unpredictable behavior. Understanding this parameter is crucial for anyone coding in Python.
Why You Should Always Use Timeouts
Many failing requests occur only if you ignore the timeouts. When your API call or Wi-Fi connection is slow, or you are scraping an HTTP requests site with hundreds of pages, your execution will freeze. Each Python request you send should have its own timeout explicitly set, so the requests library knows when to stop waiting for a response. But a timeout session allows your apps to handle poorly performing servers or non-existent API responses more swiftly. It will also get a problem instead of waiting indefinitely, giving feedback rather than just being silent. When clients make many HTTP requests calls or get proxies, timeouts stop them from getting blocked and reduce the number of failing requests caused by unstable networking. Experienced Python programmers often use built-in tools to test Python request behavior under slow APIs before deploying to production.
How to Set Timeouts in Python Requests
You can set a parameter globally or per request in your Python code. The best practice is to always define a timeout rather than rely on the library’s default. In Python, every request can have its own value. This makes Python request handling predictable and easier to debug.
For handling timeouts in your Python applications requests, you must get the timeout parameters in your request, with default values of ‘None’ that indicates no timeout session at all. You control how long the code waits by specifying this value directly in each call — the timeout applies only to that specific request.
Setting a Timeout for a Single Request
Most Python scripts include several requests, each with a custom value. That’s how Python keeps different calls responsive. To set a timeout for one request, pass it directly into the call.
For example:
requests.get("https://api.example.com", timeout=5)
This example for a python requests timeout uses a 5 second timeout. They’re specified in seconds, with fractions such as 0.5 being valid. Brief timeouts will handle GET or HEAD requests, but POST requests with data will require a larger timeout period. If a timeout isn’t specified, it will wait indefinitely, awaiting a callback or connection from a phone or a hung server. This Python snippet clearly shows how to set timeout for any single request made with the request’s library.
Setting Separate Connect and Read Timeouts
Both phases can now be controlled with separate connection and read timeouts. Each request can have different connect and read settings. Fine-tuning requests like this keep your app flexible. Use a tuple:
requests.get("https://api.example.com", timeout=(3, 10))
Important role in this process is played by timeouts that are set at 3 seconds for connection attempts and 10 seconds for reading data. This helps skip unresolved servers faster, as well as accommodate slow servers’ responses for a more realistic connection time during complicated connects.
Handling Timeout Exceptions
When coding in Python, remember that each timeout you set should match your network speed. Small timeout values keep your requests responsive. For timeout exceptions, your task needs a try/except block. For error handling, it’s imperative to log errors, handle returns safely, as well as avoid total failures. Unlike other exceptions, a timeout indicates that ‘the server didn’t answer’ as opposed to ‘the code has a problem.’ Many Python developers test their requests using mock servers to simulate timeouts. This practice improves reliability in larger Python projects.
Common Timeout Exceptions
Always retry a request only if the error type allows it. Continuous requests without limits can flood your network. Requests Library uses several Timeout Exceptions. requests.exceptions.Timeout includes any kind of timeout exception. ConnectTimeout indicates a connection that was just too slow, or a ReadTimeout if data stopped being received. Also, a general RequestException may occur for more general functions. Always include clear error messages and record when a setting was changed in your logs. Some connect or read timeouts may just safely retry, but most likely mean more serious exceptions or networking problems.
Using Sessions to Apply Default Timeouts
A Python session makes it easy to set one timeout parameter for all outgoing requests. This keeps every Python application predictable and fast. A session object in the request’s library allows you to re-use connections, as opposed to setting connect for all methods call. With session objects, you get to set a default timeout value that you do not need to re-enter for every request. Most clients get a helper function for Session.request that uses different timeouts depending on different contexts. This helps maintain consistency in your application with all timeout logic contained in a reusable object. Complex Python APIs and automation tools rely on sessions to send hundreds of requests efficiently through shared connections.
Timeouts in Asynchronous or Multi-Threaded Environments
In async Python scripts, you can still set timeouts for each task. This ensures every request completes or fails cleanly instead of freezing. Timeouts will behave in a different manner for asynchronous or multi-threaded environments. For ThreadPoolExecutor, each thread will utilize a standard requests timeout. For asyncio or aiohttp, add a global task time limit with future.result(timeout=10). For asyncio or aiohttp, utilizeClientTimeout(total=30, connect=5, sock_read=20) for control over all connections. total will control the whole call, whereas ‘connect’ and ‘read’ will handle separate stages for each. Set overall timeouts for most API calls with smaller values, and for most networking tasks, which might require a wait before receiving data, use longer timeouts. Async Python frameworks like FastAPI or aiohttp handle requests concurrently. Correct timeout settings keep those Python processes from blocking.
Common Timeout Issues and How to Fix Them
These mistakes often happen when requests are chained without timeouts. Every Python request should specify limits before execution. These Python timeout problems often appear when you forget to set correct values for your requests. Always tune this parameter to match server speed. Below are some of the most encountered problems, as well as tips on how to handle them.
Missing Timeout Handling
This will result in your code hanging indefinitely if a connection timeout isn’t provided. Additionally, if a connection or its closure generates an exception, it may bring down a whole application if such exceptions are not handled. It will require very few changes to handle such exceptions, such as having defaults, a shared session, or a retry mechanism with a delay.
Network Fluctuations and Proxy Latency
If many requests go through unstable proxies, queue them to avoid overload. Each request can reuse sessions for better stability. Some networking issues may result from a flaky connection or a proxy latency issue. Mobile or proxy networking will suffer from more delays, so it’s best to increase time-outs to preserve real connections. Fine-tune your profiles depending on individual API or large HTTP requests. This balance prevents getting blocked while avoiding constant disconnects during unstable networking conditions.
Slow DNS Resolution or API Endpoints
DNS lag can make repeated requests stack up. Tune your request timeout accordingly to prevent system-wide delays. If your lookups take a long time, this will slow down your first step for a given request, so you connect timeouts need to be higher. If your servers send a lot of data slowly, your read timeouts need to be increased, or your overall limits need to be extended.
Timeout Best Practices
For best practices, it’s best to set timeouts explicitly, as opposed to just relying on defaults. For connecting as well as for reading, it’s best to use distinct values for both. Clients should log both time and error messages for easier debugging. When it comes to retrying, it’s best to add jitter or backoff, as opposed to retrying indefinitely. In every Python request, always set a clear timeout. Well-tuned timeout values reduce retries and improve Python applications stability. Modern Python teams often maintain configuration files that define timeout rules for all Python requests inside the project.
Practical Examples
Now, let’s see a few examples that illustrate typical coding patterns for applying timeouts. You can set timeouts in these Python examples the same way — by adding a timeout parameter inside each request call. These patterns apply to every Python request, whether it’s an API request, a file download, or a local service call.
Example 1: Web Scraping
In making HTTP requests for scraping, it is best to get a timeout dictionary for each domain. Use tighter limits for fast sites, as well as larger limits for heavy pages. Employ session with an adapter that supports retry logic for responses with 429 or 5xx statuses. Record response times for monitoring performance issues or slow hosts. When requests are made in parallel, assign a timeout for each thread or session. That way, no single request blocks the rest.
Example 2: API Automation (GitHub API)
While automating API workflows such as a GitHub API, it should set different timeout values for GET requests and POST requests. GET requests may require smaller timeouts, but for large data sent in POST requests, larger timeouts should be allowed. Catch timeout exceptions and try once or twice to avoid data loss. Track returns and fails in logs for clear messaging and performance insights. This pattern repeats every automation request. You can batch requests safely if each has its own timeout rule.
Example 3: Local Application
In local usages or for microservices, it is a good idea to get much shorter timeouts, as latency will be lower. It’s a good idea to enforce a hard retry count to prevent infinite retries within a LAN. It’s also a good idea to create notifications for timeout errors or slow responses, which usually indicate a problem with local services.
Third-Party Alternatives to Python Requests
Some other libraries’ sessions provide more sophisticated timeout handling. HTTPX provides both sync and async APIs with a single, coherent timeout paradigm. Aiohttp offers fine-grained control for sophisticated async use cases. Urllib3 allows for low-level control over HTTP requests connection logic. They will get in handy in situations that require more flexibility or non-blocking requests beyond just Requests. Even when using other libraries, Python requests remain a common baseline for testing. Comparing request times helps evaluate each tool.
Conclusion
It is important to always begin with defined timeout sessions, get a safe separation between connects and reads. Timing information must be logged, as a retry mechanism must be employed with caution to maintain your application’s integrity. Applications that require high networking speeds will benefit from ProxyWing, which helps reduce lag, decreases requests failing, and improves server responses. Always remember: Python requests timeout is not optional. You should set it for every request your Python application makes.
In This Article
- What Are Timeouts in Python Requests?
- Why You Should Always Use Timeouts
- How to Set Timeouts in Python Requests
- Handling Timeout Exceptions
- Using Sessions to Apply Default Timeouts
- Timeouts in Asynchronous or Multi-Threaded Environments
- Common Timeout Issues and How to Fix Them
- Timeout Best Practices
- Practical Examples
- Third-Party Alternatives to Python Requests
- Conclusion
Ready to get started?
Related posts


