Ryan K
Ryan K

Reputation: 1

Running multiple python scripts at the same time

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.

Upvotes: 0

Views: 1323

Answers (1)

wp78de
wp78de

Reputation: 18990

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")

Upvotes: 1

Related Questions