Rest of code won't execute until launched programme is closed [duplicate] - python

This question already has answers here:
Non blocking subprocess.call
(5 answers)
Closed 4 years ago.
I'm launching a programme with subprocess and follow that command with a for loop. The loop won't run until the programme I launched is closed. I don't understand why this is. Could someone please explain?
My Code:
import subprocess
import psutil
subprocess.call('/path_to/programme.exe')
for process in psutil.process_iter():
print(process)
I'm running Raspbian OS on a Pi 3 (armv7l).
Thanks for your time.
L

subprocess.call will wait until the command completes. Use subprocess.Popen instead.
This question is a duplicate of Non blocking subprocess.call

Related

Cleanly finish Python 3.6 application at shutdown on Windows [duplicate]

This question already has answers here:
Python - Windows Shutdown Events
(2 answers)
Python handling system shutdown
(1 answer)
Detecting computer/program shutdown in Python?
(2 answers)
Python Save Sets To File On Windows Shutdown?
(2 answers)
Closed 4 years ago.
I would like to know how to "cleanly" finish the execution of a python application on Windows shutdown.
My application starts at startup, opens a text file and keeps running while Windows is on, What I want to do is to, somehow, capture the shutdown signal and inmediately stop the processes running and close the text file with the io method myFile.close(), finishing the program by itself, and not forced by Windows. Any idea about how that could be done?
Thank you in advance.
Regards

How to close linux terminal with python script [duplicate]

This question already has answers here:
How to Exit Linux terminal using Python script?
(2 answers)
kill process with python
(2 answers)
Closed 6 years ago.
Just wondering if there's a way to close the terminal with a python script in an if else statement. I know how to exit a program with "exit( 0 )" but is there a way to actually close the terminal, like when you hit "X" or Alt + F4?
Thanks! (I'm new to python so I don't know much, sorry.)

How to run batch file using python script? [duplicate]

This question already has answers here:
Run a .bat file using python code
(9 answers)
Closed 6 years ago.
Is this possible? I yes, can anyone help me how to do it. I don't know how to use subprocess and Popen() to run my sort.bat file.
You can use os.system:
import os
os.system('Path to your .bat file')

how to run my own external command in python script [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Run a linux system command as a superuser, using a python script
(6 answers)
Closed 9 years ago.
I wanna to run my own non-system external commands in python.
Such as "sudo insteon on 23". Subprocess and os.system are designed for system calls.
Does anybody know how to do it?
Thanks
You can use subprocess.Popen for this:
import shlex
import subprocess
proc = subprocess.Popen(shlex.split('sudo insteon on 23'))
proc.communicate()

Command Line Execution in Python with subprocess? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Python subprocess
child subprocess kill in python daemon
How can I run commands in the windows command line through python?
I'm trying to have it so that it starts a subprocess, I don't care about the output, but I want to be able to kill it at any time.
subprocess.Popen: http://docs.python.org/library/subprocess.html

Categories

Resources