I need to create a python script that monitors another python script, specifically, some variables from it. I was thinking of creating an independent thread in the "observer script" that does this. This thread must run until the "executer script" finishes. Maybe all of this can be done inside one big script (observer+executer), I really do not know at the moment.
Is it possible at all to do this with python in windows?
Related
In my code I would like to launch a function/script in another python window (say, when you run one script, a back window pops up, I want that script to manage other scripts. They don't need to communicate).
Similar to multiprocessing but they have their own pop up windows and outputs. All their information is going to be written to a file there after.
I have searched a fair amount but it seems like no one want a script to run another script in another window, potentially running 4 or 5 python windows consecutively, each using a separate core.
You should be able to use os.startfile(filename) Here is an example that runs another python file:
import os
os.startfile("File.py")
print("Started Running!")
This will open and run another python program, allowing this program to continue running.
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.
I'm developing a script in PyScripter. When I run it in PyScripter it runs fairly well.
However, the script contains two separate threads (one Thread object, and the main flow of the script). When I run the script from the Command prompt it gets stuck in the Thread. It gives no impression of executeing the main process, and it never ends, which it does when I run it inside PyScripter. What should I do?
In your code, use timeout in join() to put time constrain on the thread. For instance
....
yourThread = threading.Thread()
yourThread.start()
yourThread.join(10.0)
....
Instructions of multithreading checks here. Hope it helps you.
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.
I have some small python 2.6 scripts built....
Now, I would like run them as seperate processes within a python shell. Each as a seperate process. If one fails to run maybe with its timer, I would like others to continue without killing all scripts.
Should I do this as singleton gui's or combine them into bigger launch pad. My perference would be launch pad type gui....Any ideas?
Its seems that launching scripts out of SciTE, works ok.
Check joblaunch, a shell tool I made for executing interdependent jobs in parallel locally. It has more options.