|
| 1 | + |
| 2 | +.. _timeout-example: |
| 3 | + |
| 4 | +Client Side Operation Timeout |
| 5 | +============================= |
| 6 | + |
| 7 | +PyMongo 4.2 introduced :meth:`~pymongo.timeout` and the ``timeoutMS`` |
| 8 | +URI and keyword argument to :class:`~pymongo.mongo_client.MongoClient`. |
| 9 | +These features allow applications to more easily limit the amount of time that |
| 10 | +one or more operations can execute before control is returned to the app. This |
| 11 | +timeout applies to all of the work done to execute the operation, including |
| 12 | +but not limited to server selection, connection checkout, serialization, and |
| 13 | +server-side execution. |
| 14 | + |
| 15 | +:meth:`~pymongo.timeout` is asyncio safe; the timeout only applies to current |
| 16 | +Task and multiple Tasks can configure different timeouts concurrently. |
| 17 | +:meth:`~pymongo.timeout` can be used identically in Motor 3.1+. |
| 18 | + |
| 19 | +For more information and troubleshooting, see the PyMongo docs on |
| 20 | +`Client Side Operation Timeout`_. |
| 21 | + |
| 22 | +.. _Client Side Operation Timeout: https://pymongo.readthedocs.io/en/stable/examples/timeouts.html |
| 23 | + |
| 24 | + |
| 25 | +Basic Usage |
| 26 | +----------- |
| 27 | + |
| 28 | +The following example uses :meth:`~pymongo.timeout` to configure a 10-second |
| 29 | +timeout for an :meth:`~pymongo.collection.Collection.insert_one` operation:: |
| 30 | + |
| 31 | + import pymongo |
| 32 | + import motor.motor_asyncio |
| 33 | + client = motor.motor_asyncio.AsyncIOMotorClient() |
| 34 | + coll = client.test.test |
| 35 | + with pymongo.timeout(10): |
| 36 | + await coll.insert_one({"name": "Nunu"}) |
| 37 | + |
| 38 | +The :meth:`~pymongo.timeout` applies to all pymongo operations within the block. |
| 39 | +The following example ensures that both the ``insert`` and the ``find`` complete |
| 40 | +within 10 seconds total, or raise a timeout error:: |
| 41 | + |
| 42 | + with pymongo.timeout(10): |
| 43 | + await coll.insert_one({"name": "Nunu"}) |
| 44 | + await coll.find_one({"name": "Nunu"}) |
| 45 | + |
| 46 | +When nesting :func:`~pymongo.timeout`, the nested deadline is capped by the outer |
| 47 | +deadline. The deadline can only be shortened, not extended. |
| 48 | +When exiting the block, the previous deadline is restored:: |
| 49 | + |
| 50 | + with pymongo.timeout(5): |
| 51 | + await coll.find_one() # Uses the 5 second deadline. |
| 52 | + with pymongo.timeout(3): |
| 53 | + await coll.find_one() # Uses the 3 second deadline. |
| 54 | + await coll.find_one() # Uses the original 5 second deadline. |
| 55 | + with pymongo.timeout(10): |
| 56 | + await coll.find_one() # Still uses the original 5 second deadline. |
| 57 | + await coll.find_one() # Uses the original 5 second deadline. |
| 58 | + |
| 59 | +Timeout errors |
| 60 | +-------------- |
| 61 | + |
| 62 | +When the :meth:`~pymongo.timeout` with-statement is entered, a deadline is set |
| 63 | +for the entire block. When that deadline is exceeded, any blocking pymongo operation |
| 64 | +will raise a timeout exception. For example:: |
| 65 | + |
| 66 | + try: |
| 67 | + with pymongo.timeout(10): |
| 68 | + await coll.insert_one({"name": "Nunu"}) |
| 69 | + await asyncio.sleep(10) |
| 70 | + # The deadline has now expired, the next operation will raise |
| 71 | + # a timeout exception. |
| 72 | + await coll.find_one({"name": "Nunu"}) |
| 73 | + except PyMongoError as exc: |
| 74 | + if exc.timeout: |
| 75 | + print(f"block timed out: {exc!r}") |
| 76 | + else: |
| 77 | + print(f"failed with non-timeout error: {exc!r}") |
| 78 | + |
| 79 | +The :attr:`pymongo.errors.PyMongoError.timeout` property (added in PyMongo 4.2) |
| 80 | +will be ``True`` when the error was caused by a timeout and ``False`` otherwise. |
0 commit comments