nap.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright 2016 Étienne Bersac
  2. # Copyright 2016 Julien Danjou
  3. # Copyright 2016 Joshua Harlow
  4. # Copyright 2013-2014 Ray Holder
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import time
  18. import typing
  19. if typing.TYPE_CHECKING:
  20. import threading
  21. def sleep(seconds: float) -> None:
  22. """
  23. Sleep strategy that delays execution for a given number of seconds.
  24. This is the default strategy, and may be mocked out for unit testing.
  25. """
  26. time.sleep(seconds)
  27. class sleep_using_event:
  28. """Sleep strategy that waits on an event to be set."""
  29. def __init__(self, event: "threading.Event") -> None:
  30. self.event = event
  31. def __call__(self, timeout: typing.Optional[float]) -> None:
  32. # NOTE(harlowja): this may *not* actually wait for timeout
  33. # seconds if the event is set (ie this may eject out early).
  34. self.event.wait(timeout=timeout)