Dan
Dan

Reputation: 81

Is there a way to make a program pause for a certain time but still have another piece of code running?

import time
time.sleep(1)

I want a for loop running while that is happening how do I do that

Upvotes: 1

Views: 48

Answers (1)

Shubham Sharma
Shubham Sharma

Reputation: 71707

You can use concurrent execution of tasks to achieve what you intend to do, Have a look at code which helps you clear your understanding of concurrent programming,

import time
from concurrent import futures

def busy():
    print("\nRunning in seprate thread")
    print("\nTask started")
    time.sleep(5)
    print("\nTask accomplished")

def main():
    executor = futures.ThreadPoolExecutor()
    executor.submit(busy)

    for i in range(100):
        print("\nRunning in main thread")
        time.sleep(1)

    executor.shutdown() # --> wait for all the tasks to get finished

main()

Upvotes: 1

Related Questions