Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Mise en oeuvre d'IHM
avec Qt et PySide6
Voir le programme détaillé
Module « pymongo »

Fonction timeout - module pymongo

Signature de la fonction timeout

def timeout(seconds: 'Optional[float]') -> 'ContextManager[None]' 

Description

help(pymongo.timeout)

**(Provisional)** Apply the given timeout for a block of operations.

.. note:: :func:`~pymongo.timeout` is currently provisional. Backwards
   incompatible changes may occur before becoming officially supported.

Use :func:`~pymongo.timeout` in a with-statement::

  with pymongo.timeout(5):
      client.db.coll.insert_one({})
      client.db.coll2.insert_one({})

When the with-statement is entered, a deadline is set for the entire
block. When that deadline is exceeded, any blocking pymongo operation
will raise a timeout exception. For example::

  try:
      with pymongo.timeout(5):
          client.db.coll.insert_one({})
          time.sleep(5)
          # The deadline has now expired, the next operation will raise
          # a timeout exception.
          client.db.coll2.insert_one({})
  except PyMongoError as exc:
      if exc.timeout:
          print(f"block timed out: {exc!r}")
      else:
          print(f"failed with non-timeout error: {exc!r}")

When nesting :func:`~pymongo.timeout`, the nested deadline is capped by
the outer deadline. The deadline can only be shortened, not extended.
When exiting the block, the previous deadline is restored::

  with pymongo.timeout(5):
      coll.find_one()  # Uses the 5 second deadline.
      with pymongo.timeout(3):
          coll.find_one() # Uses the 3 second deadline.
      coll.find_one()  # Uses the original 5 second deadline.
      with pymongo.timeout(10):
          coll.find_one()  # Still uses the original 5 second deadline.
      coll.find_one()  # Uses the original 5 second deadline.

:param seconds: A non-negative floating point number expressing seconds, or None.

:raises: :py:class:`ValueError`: When `seconds` is negative.

See :ref:`timeout-example` for more examples.

.. versionadded:: 4.2


Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les compléments
Voir le programme détaillé