Mani
Mani

Reputation: 47

Having CMD commands run in background

I'm working on a python program. I want to run commands on cmd and have my result back in the program without notifying the user and also not opening the cmd window. So the whole progress should be in the background,and the output must be printed in my own program interface. I found OS module, does it satisfy my two demands? If yes, how? Thanks.

Upvotes: 0

Views: 160

Answers (1)

Dushyant Bhardwaj
Dushyant Bhardwaj

Reputation: 178

You can use threads to do stuff in the background. In python, one way of doing that is via the threading module.

from threading import Thread
import time

def do_stuff():
    time.sleep(5)
    print("tasks being done in the backgroud")
    print("Outputting Result to user")

print("Program Begin")
t1 = Thread(target=do_stuff)
t1.start()
while(True):
    print("Program doing other stuff")
    time.sleep(1)
    if t1.is_alive() == False:
        break

Here we have a function do_stuff() that does something. Now, with help of threads, we can run it in the background and do other things meanwhile.

Upvotes: 1

Related Questions