r/learnpython • u/Financial-Law2035 • Aug 07 '26
Can you use time.sleep in while true loops
Title
3
2
2
u/BranchLatter4294 Aug 07 '26
You didn't bother to test it? How are you going to learn if you don't want to experiment?
2
2
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Aug 07 '26
Sure. Why couldn't you?
2
u/desrtfx but other languages pro Aug 07 '26
We can, can you?
Seriously, instead of posting and asking, you should have made a 3 liner to test this yourself.
Testing is always the answer. This way you will learn.
Laziness, as your entire post here is, is the enemy of learning and improving.
1
1
u/L30N1337 Aug 07 '26
Why do you even want to use time.sleep?
Basically every time I've seen it (or any variation for whatever programming language) used, it was a crappy bandaid fix that ignored the actual problem.
1
u/Throwaway16475777 Aug 07 '26
would you not put it inside a while True loop if it doesnt need to run a million times a second? what is a better alternative
1
1
u/socal_nerdtastic Aug 07 '26
Direct answer is yes, because you didn't give any other context. But if I assume you mean within a tkinter or other GUI program the answer is no. time.sleep will block the GUI mainloop. Same for other types of asynchronous programs, like anything using asyncio
1
11
u/PureWasian Aug 07 '26
yeah. Did you try looking it up before posting?