nap.py 1.2 KB

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