Search code examples
pythonpython-os

Why does os.system block program execution?


I am trying to create a program to easily handle IT requests, and I have created a program to test if a PC on my network is active from a list.

To do this, I wrote the following code:

self.btn_Ping.clicked.connect(self.ping)

def ping(self):
        hostname = self.listWidget.currentItem().text()
        if hostname:
            os.system("ping " + hostname + " -t")

When I run it my main program freezes and I can't do anything until I close the ping command window. What can I do about this? Is there any other command I can use to try to ping a machine without making my main program freeze?


Solution

  • The docs state that os.system() returns the value returned by the command you called, therefore blocking your program until it exits.

    They also state that you should use the subprocess module instead.