Guerra09
Guerra09

Reputation: 1

How to open a program in a script and keep running the script?

I've been trying to use os.system to open Xampp automatically and then run my script made with tkinter. But the problem is that the script stops until I close manually the subprocess.

Here an example.

import os

direccion = 'C:/xampp/xampp-control.exe'
os.system(direccion)

print("Hello world")

The print() is not working, and just starts to run when I close xampp manually. What can I do to, at the same time is opening xampp, keep running the script?

Upvotes: -1

Views: 85

Answers (2)

AlexxRR
AlexxRR

Reputation: 11

I found a solution working for me:

import os
import subprocess

def SomeFunction():
    subprocess.Popen([myAppWithPath])
    time.sleep(5)
    os.system("taskkill /f /im MyApp.exe")
    ...
    # continue with the script...

A bit roughly written but it´s quite easy to figure out (once you know).

Upvotes: 0

Ali
Ali

Reputation: 1495

in python codes executed line by line...

there is two ways to do that:

first and the best way is to wrap the code to a function...

e.g.

def opener(direccion: str):
    os.system(direccion)

then use opener function whenever and wherever you want! (e.g. after or abobe your print statement)

second way is to use threading modules...

e.g.

import os
import threading
import concurrent.futures

def opener(direccion: str):
        os.system(direccion)

def printer():
    print("Hello World!")

with concurrent.futures.ThreadPoolExecutor() as executor:
    f1 = executor.submit(opener, "C:/xampp/xampp-control.exe")
    f2 = executor.submit(printer)
    print(f1.result())
    print(f2.result())

more info about threading & concurrent.futures modules

Upvotes: 0

Related Questions