r/pythonhelp • • 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 comments sorted by

2

u/carcigenicate Feb 23 '22

time.sleep isn'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 use asyncio.sleep, which basically just lets another part of your program run. That way, you could make other requests during the sleep. 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.

1

u/[deleted] Feb 23 '22

Hell yeah perfect reply. I was going to make a few functions call, get prices, pause then loop back. No gui just using time.sleep, requests, bs4. I have just seen so many people argue over how to pause it's just confusing