r/pythonhelp • u/[deleted] • Feb 22 '22
Right way to pause a script?
I'm making a script that will pull crypto prices and a few stock prices every so many minutes. Last script I wrote I had a few people saying time.sleep(60) is bad then system("pause") is bad. Wtf is the right way to pause a script? Have seen several people say this and that I just want y'all's opinion.
3
Upvotes
2
u/carcigenicate Feb 23 '22
time.sleepisn't bad if you use it right. If you don't care about being exactly accurate, and you don't need the thread for anything else, it works fine. The main problem with it is it prevents the thread from running, which in many circumstances is not a good thing (like if the code is in a UI app, and you attempt to sleep a UI thread).To make better use of the thread though, you can use a HTTP request library that supports
asyncio, and useasyncio.sleep, which basically just lets another part of your program run. That way, you could make other requests during thesleep. If you're currently sequentially pulling multiple prices, you could instead create a new job for each task and have them run semi in parallel.