|
| 1 | +--- |
| 2 | +id: timeouts |
| 3 | +title: Timeouts |
| 4 | +description: Configure the tiered timeout system for controlling how long API requests can take. |
| 5 | +--- |
| 6 | + |
| 7 | +import Tabs from '@theme/Tabs'; |
| 8 | +import TabItem from '@theme/TabItem'; |
| 9 | +import CodeBlock from '@theme/CodeBlock'; |
| 10 | +import ApiLink from '@site/src/components/ApiLink'; |
| 11 | + |
| 12 | +import TimeoutsAsyncExample from '!!raw-loader!./code/11_timeouts_async.py'; |
| 13 | +import TimeoutsSyncExample from '!!raw-loader!./code/11_timeouts_sync.py'; |
| 14 | + |
| 15 | +The Apify client uses a tiered timeout system to set appropriate time limits for different types of API requests. Each tier has a default value suited to its use case: |
| 16 | + |
| 17 | +| Tier | Default | Purpose | |
| 18 | +|---|---|---| |
| 19 | +| `short` | 5 seconds | Fast CRUD operations (get, update, delete) | |
| 20 | +| `medium` | 30 seconds | Batch, list, and data transfer operations | |
| 21 | +| `long` | 360 seconds | Long-polling, streaming, and heavy operations | |
| 22 | +| `no_timeout` | — | Disables the timeout entirely | |
| 23 | + |
| 24 | +Every client method has a pre-assigned tier that matches the expected duration of the underlying API call. You generally don't need to change these unless you're working with unusually large payloads or slow network conditions. |
| 25 | + |
| 26 | +## Configuring default timeouts |
| 27 | + |
| 28 | +You can override the default values for each tier in the <ApiLink to="class/ApifyClient">`ApifyClient`</ApiLink> or <ApiLink to="class/ApifyClientAsync">`ApifyClientAsync`</ApiLink> constructor. The `timeout_max` parameter sets an upper cap on the timeout for any individual API request, limiting exponential growth during retries. |
| 29 | + |
| 30 | +<Tabs> |
| 31 | + <TabItem value="AsyncExample" label="Async client" default> |
| 32 | + <CodeBlock className="language-python"> |
| 33 | + {TimeoutsAsyncExample} |
| 34 | + </CodeBlock> |
| 35 | + </TabItem> |
| 36 | + <TabItem value="SyncExample" label="Sync client"> |
| 37 | + <CodeBlock className="language-python"> |
| 38 | + {TimeoutsSyncExample} |
| 39 | + </CodeBlock> |
| 40 | + </TabItem> |
| 41 | +</Tabs> |
| 42 | + |
| 43 | +## Per-call overrides |
| 44 | + |
| 45 | +Most client methods accept a `timeout` parameter that overrides the default tier for that specific call. You can pass either a `timedelta` for an exact duration or a tier literal (`'short'`, `'medium'`, `'long'`, `'no_timeout'`) to switch tiers. |
| 46 | + |
| 47 | +```python |
| 48 | +from datetime import timedelta |
| 49 | + |
| 50 | +# Use an exact timeout for this call. |
| 51 | +client.dataset('id').list_items(timeout=timedelta(seconds=120)) |
| 52 | + |
| 53 | +# Switch to a different tier. |
| 54 | +client.dataset('id').list_items(timeout='long') |
| 55 | + |
| 56 | +# Disable the timeout entirely. |
| 57 | +client.dataset('id').list_items(timeout='no_timeout') |
| 58 | +``` |
| 59 | + |
| 60 | +## Interaction with retries |
| 61 | + |
| 62 | +Timeouts work together with the [retry system](/api/client/python/docs/concepts/retries). When a request times out, it counts as a failed attempt and triggers a retry (up to `max_retries`). The timeout applies to each individual attempt, not the total time across all retries. |
0 commit comments