Terminating other python script with python - python

Sorry, If I'm a little bit unclear.
I'm writing a module with python, and its basic functions must be run or shut, which runs or terminates other specified python scripts correspondingly.
Run is realised by os.popen(), but I did not manage to find any possible realisation for shut command. Is there any built-in function in python, which allows you to terminate other scripts?

Related

How do I debug through a gdb helper script written in python?

there may very well be an answer to this question, but it's really hard to google for.
you can add commands to gdb by writing them in python. I am interested in debugging one of those python scripts that's running in gdb session.
my best guess is to run gdb on gdb and execute the user added command and somehow magically break on the python program code?
has anybody done anything like this before? I don't know the mechanism by which gdb calls python code, so if it's not in the same process space as the gdb that's calling it, I don't see how I'd be able to set breakpoints in the python program.
or do I somehow get pdb to run in gdb? I guess I can put pdb.set_trace() in the python program, but here's the extra catch: I'd like to be able to do all this from vscode.
so I guess my question is: what order of what things do I need to run to be able to vscode debug a python script that was initiated by gdb?
anybody have any idea?
thanks.
so I figured it out. it's kinda neat.
you run gdb to debug your program as normal, then in another window you attach to a running python program.
in this case the running python program is the gdb process.
once you attach, you can set breakpoints in the python program, and then when you run commands in the first window where the gdb session is, if it hits a breakpoint in the python code, it will pop up in the second window.
the tipoff was that when you run gdb there does not appear to be any other python process that's a child of gdb or related anywhere, so I figured gdb must dynamically link to some python library so that the python compiler/interpreter must be running in the gdb process space, so I figured I'd try attaching to that, and it worked.

How to intercept running python script and execute command within its context?

Here is the situation (as an example) - I ran a ML learning python script (which I wrote) for a long time but I didn't add functionality to save its weights.
My question in this situation is if it's possible to somehow intercept the running python interpreter and execute a command within the program context.
For example since I have model_seq global variable inside the running program - I would like to execute:
model_seq.save_weights("./model_weights")
Inside the process.
I've heard this is somewhat possible with gdb.
(Personally I know this can be done for sure with a C program and gdb - but since Python is compiled and interpreted the steps are a bit unclear for me (and I'm not sure if I would actually need a special python3 build or the default ubuntu one I have running will work))

running command line in multiple running processes from python

I'm not sure if what I'm wanting to do is possible, but:
I have a python script (lets call it PY) that calls a batch script to start a tool in terminal mode (lets call it A). This tool gets passed a starting script (tcl script) that sets up its environment and launches a second tool (lets call it B). The two tools communicate over a TCP connection locally.
My question is, with these two programs running (A and B), can I switch back to the python script to run commands in either A or B's TCL interface?
The scripts look sort of like this:
#python PY
def ReadConigAndSetup():
#read some data
...
#run bat
subprocess.run("./some_bat.bat some_data_args")
#bat start program A and pass it a startup script
some_program_A -mode tcl -source ./some_source.tcl
#tcl some_source.tcl
setup environment
open TCP port
start program B
#program B setup tcl
some more setup
after program B has run I'd like to be able to run more commands in program B from python as parsing some of the config files is much easier in the python environment.
The answer is “it depends on the details”.
There's no reason in principle why the program being called can't work fine this way, provided the subprocess relinquishes control back (which it might or might not), but launching complex programs via a BAT file is adding an extra layer of complexity so you might want to think about whether you can simplify a bit there.
If the program running the Tcl code doesn't terminate, things get trickier. This is an area where the details are critical; Tcl code can be written to loop indefinitely — it's a programming language so of course it can be told to be annoying if you insist — and the program being controlled could also decide to loop indefinitely of its own accord, which can happen particularly with GUI applications as the looping is where the user is interacting with the GUI. On Windows, many GUI applications run disconnected from the terminal (whether they do this is a compile-time option) and waiting for them to finish can be quite annoying.
It's possible to run multiple subprocesses at once using subprocess.Popen. Be very careful if you do this. It's possible to get into deadlocks (though that depends a lot on what the subprocesses are doing). It's probably easier to just launch each subprocess from its own thread… but then you're dealing with threads and that's also complicated.

Running a python app alongside the live interpereter

I'm making a drawing program with python and pygame. I am trying to incorporate a script-fu thing in which the program opens a python live interpreter upon startup and allows the user to execute commands in the interpreter alongside the graphical interface.
My current strategy is to run the main loop inside its own thread, then have the application opened using a bash script that does 'python -i main.py'
Is this a safe/effective/ideal way of doing this? How can I use locks to ensure that commands coming in from the interpreter are executed between main loop iterations?
This is my first time using threads, so please explain to me like I am 7.
Thanks :)
The interpreter won't cooperate with locks you set (since it doesn't know about them). Thus, you cannot guarantee when the code entered by the user will execute.
Consider using the code module to build your own interactive console (it's really easy!). Then you can do the locking every time you go to execute user input.
Why are you using a third party live interpreter? Do you realize that pygame comes with one built in? The documentation is here. This will eliminate all of your problems quite easily.

python programming query

I'm new to Python programming.
My question is where to write python programs in linux--
in terminal or
any writing pad like gedit etc
Should we write one program again and again for running or we should call the program and run it.
After you install Python (it's installed by default on most Linux-es), you have two options:
Terminal. Just type python <ENTER> and you will be taken to the Python interpreter interactive prompt. For anything but the most basic stuff, however, you may want to intall IPython - an alternative interactive prompt that's much better than the default one.
In a file. Save your code into a .py file and run it with python myfile.py
But first and foremost, start by learning Python - the official tutorial is a great place to start. Among other useful information, its chapter 2 discusses how to use the Python interpreter for beginners.
Stackoverflow also has a lot of great resources for learning Python. This question, for example, and many others.
If you are learning, or you are evaluating expressions, you could run Python in terminal, or use IDLE.
But if you are writing large chunks of code, then you should consider using an IDE.
You could either use Geany, or use Eclipse with PyDev. I prefer Eclipse myself.
For running, you can run it using the command python program.py, or just add the line
#!/bin/python
to the beginning of your program, grant it execution permission using chmod, and run it with ./program.py command.

Categories

Resources