Can we launch a parallel shell command from python? - python

I want to use a go executable: timescaledb-parallel-copy to insert data into database from a csv file. However, I plan to use Python for reading in the filename and lookup the appropriate table name for insertion. If I then launch timescaledb-parallel-copy as a Python subprocess to execute on shell, will it still be parallel? I do not need Python to make it parallel, it is parallel by default. I just do not want Python to make it single-threaded.

If you are using subprocess.run() then your program, timescaledb-parallel-copy will execute as if you had called it from the shell. It will still be in parallel. The python script will not be, and will wait on timescaledb-parallel-copy to return.

Yes, I believe it will be. By launching the program as a subprocess, you are running the program as it originally would be, with no interference from python.

Related

Run python interactive console as a subprocess in swift with sending commands and receiving results after launched

I'm making my first steps in macOS app development.
I'm trying to write an app on Swift that would keep python interactive console open.
Sometimes I would like to send to python commands and return the results back to swift, but not closing python to keep all variables for the next command I will send.
Is there any way to do that?
As far as I understand, I can't use the Process() because the input pipe automatically closes when I run the task.
I probably need to use pseudo terminals with pty and tty, but I don't fully understand the idea and where to learn about it. (or, maybe, I'm wrong and there is another way)
If you actually want to use python code from swift, I would strongly advise you to avoid using this method. It is very bug prone and potentially limiting and inefficient. You better use some wrapper of the python-c-api, or write some small server in python to receive requests from swift.
If you still want to do that, an easy way to go about it would be to use python itself to spawn python inside a pty:
python -c "import pty, sys; pty.spawn(sys.argv[1:])" python
This will start a python console that reads and writes to stdio instead of of /dev/tty.

Running python script from SAS Enterprise Guide

I need some help running a python script from inside SAS. I want to take advantage of the possibility of scheduling a SAS code to run every day, I already have a script in python that manipulates everything I need to do. Is there a way to host the python script into SAS and make it run every day without the need of my pc to be turned on?
You need to verify if you have the XCMD option enabled. If it's enabled, your log will show XCMD and then you can use an X statement to run your python scripts.
proc options option=xcmd;
run;
Next, find the command line that you'll need to run the scripts from the command line. Then place that after an X in your code or use CALL SYSTEM() or X to execute the command. Hardest part is usually making sure the quotes are correctly resolved.
x 'command line command goes here';

Using os.system() in Python is open a program, can't see window of launched program

I am trying to launch a program/GUI from within a python code.
From the terminal, I can get the program to launch by simply typing the program name. A few lines get outputted to the terminal, and then a separate window opens with the GUI.
I tried to emulate this in python by running
os.system("<program name>")
The typical output lines, as mentioned above, get printed to the console, but no window opens up with the GUI.
Can os.system() be used to execute programs that have their own separate window?
From the Python manual:
[os.system] is implemented by calling the Standard C function
system()
That being said, you shouldn't have any problems launching a GUI application with os.system. I've just tried it myself and it works fine.
It also mentions in the manual that:
The subprocess module provides more powerful facilities for spawning
new processes and retrieving their results; using that module is
preferable to using this function.
Maybe that's worth a try. Do any other GUI applications work when you spawn them with os.system?
Here is a solution using subprocess
import subprocess
subprocess.Popen("notepad.exe")
Or if you want to run a python program with a specific interpreter:
subprocess.Popen('{0} {1}'.format(PythonInterpreterPath,PythonFilePath.py))

Python subprocess running out of order on Windows

I'm running a python script on Windows.
I have a python script like this:
subprocess.call(1)
subprocess.Popen(2)
subprocess.call(3)
when I run the script, the results I get runs like this:
subprocess.call(3)
subprocess.call(1)
subprocess.Popen(2)
Why is this happening?
Each new process you create with subprocess spawns a new sub-process, hence its name. This means the commands will finish running at different times meaning you get the results in a different order.
It is not the same as calling a function in Python, where the function finishes running before the others are ran.

Is it possible to use batch scripts in a GUI made with Python?

I was wondering if it was possible to write a GUI in python, and then somewhere in the python script, insert a script switch to temporarily change the language to accomodate for the batch snippet.
I know this can be done in html and vbscript but what about Python?
You can control other processes, written with any language, including bash using the subprocess module.
The subprocess module is the most powerful and complete method for executing other processes. However, there's also a very simple method using the os module: os.system(command) runs command just as if you were to type it into a command line.

Categories