I'm making a program which contains uses a lot of time.sleep()
... I was wondering if there was a way I could disable thetime.sleep()
s temporarily (for example, if a certain argument is passed, time.sleep()
s don't run)

- 119,623
- 25
- 170
- 301

- 23
- 1
-
3`def foo(x): if x == 'sleep': time.sleep(100)` – cs95 Sep 09 '17 at 23:01
-
Why or how would this possibly work? – Philip Jackoson Sep 09 '17 at 23:08
-
Consider googling for "Python mocking framework". In Python 3, [`unittest.mock`](https://docs.python.org/3/library/unittest.mock.html) is even part of the standard library. Patch in the mock object in place of the real one (which, again, any decent mocking framework will do for you with a context manager to *undo* the change later -- no need to do it by hand as the accepted answer does), and there you are. – Charles Duffy Sep 09 '17 at 23:17
-
(See also pytest [`monkeypatch`](https://docs.pytest.org/en/latest/monkeypatch.html)). – Charles Duffy Sep 09 '17 at 23:21
-
(I'm tempted to flag this duplicate of https://stackoverflow.com/questions/38159765/mocking-a-python-standard-library-function-with-and-without-pytest-mock -- presumably you didn't find it on account of not being familiar with the relevant terminology for replacing a function with a dummy that behaves in a way more amenable to testing, but it *is* asking how to accomplish the thing that's your goal here; and inasmuch as the utility of duplicates is finding ways for folks with different terminology/phrasing to find canonical answers to their questions, there's value in having such a pointer). – Charles Duffy Sep 09 '17 at 23:23
-
1For somebody who isn't experienced in Python, I wouldn't have found that, thus this question was asked. – Philip Jackoson Sep 09 '17 at 23:27
3 Answers
If your program does not need to use the time module for anything else you could make a new python file called time.py and just write one line in it that reads pass
. Save and place that in the same directory as your other file. additionally if you made a variable and multiplied it to every time.sleep. eg: time.sleep(100*x)
you could set the variable to zero and to remove the sleeps or to 1 to make them normal

- 714
- 1
- 6
- 19
I don’t see why you have to go through so much trouble. You can simply save the original time.sleep
, override the time.sleep
when you want to disable it. For certain places that you wanted time.sleep
to take place no matter what, just use the backup time.sleep
instead.
import time
org_sleep = time.sleep # save the original time.sleep
sleep = False # set sleep to false when doesn’t want to sleep
if sleep != True: # if sleep is false
# override time.sleep to an empty function
time.sleep = lambda x: None
time.sleep(10) # does not sleep
print("didn’t sleep")
org_sleep(10) # will sleep regardless of sleep being set to true or false
print("slept for 10 seconds")
Furthermore, as Charles Duffy suggested, using pytest’s monkeypatch to mock time.sleep
is the preferred way to go.

- 31,927
- 11
- 74
- 85
-
Despite what @Charles Duffy said, IMO there's little reason to use pytest's monkeypatch in this case because doing it yourself would be so trivial. See question [_What is a monkey patch?_](https://stackoverflow.com/questions/5626193/what-is-a-monkey-patch). – martineau Sep 10 '17 at 01:06
import time
time.oldsleep = time.sleep
# Set or clear this flag to enable/disable sleep
time.cansleep = True
def mysleep(x):
if time.cansleep:
time.oldsleep(x)
time.sleep = mysleep
Edit: hogwash removed. modules are singletons. TIL.

- 784
- 6
- 8
-
2modules are singletons, you modify `seep` in the `time` module, so the change is visible everywhere, where you use `time.sleep`. Only exception is, when you use `from time import sleep` before you have done the patch. – Daniel Sep 09 '17 at 23:17
-
Philip: the part of this answer about needing to make the changes any place you `import` the module again is untrue—subsequent attempts to do the same thing would result in `time.oldsleep` being set to previous already overridden version of the function—which, although it would "work", would be completely unnecessary (as well as likely confusing to anyone who actually understands how Python handles the loading of modules). – martineau Sep 10 '17 at 01:25
-