I am new to python coding (using pyscripter with the latest version of python (3.5)). I am wondering how to run 2 loops or scripts at the same time. IM using the graphics.py module and the time module. I need to open a window, create a countdown, and do a bunch of other stuff at the same time, but the problem is when i try t create a countdown using "time.sleep", it pauses the rest of the program. How would i go about creating a timer/countdown without pausing the rest of my script? Also it needs to be able to draw on the same window. Thank you very much.
-
1You might want to look into the pygame module: pygame.org/wiki/GettingStarted . It's not just for games - it's a generic event loop.J_H– J_H2017-09-25 00:59:41 +00:00Commented Sep 25, 2017 at 0:59
-
Welcome on stackoverflow.com. Please always try to provide a Minimal, Complete, and Verifiable example that shows where you got stuck.wp78de– wp78de2017-09-25 01:08:47 +00:00Commented Sep 25, 2017 at 1:08
Add a comment
|
1 Answer
You can create a (worker) thread to do some extra work without blocking the rest of your code.
Here is some basic code (below) to get you started and a how-to.
from threading import Thread
from time import sleep
def threaded_function(arg):
for i in range(arg):
print("running")
sleep(1)
if __name__ == "__main__":
thread = Thread(target = threaded_function, args = (10, ))
thread.start()
thread.join()
print("thread finished...exiting")