You are not logged in.
OK, I'm still new to python (but my teeth on COBOL a long long time ago), and whilst I can usually battle through existing scripts and figure out what's going on, I'm trying to add functionality to one of my own and no idea how to do it.
I have a python script that launches an external process (os.popen) and then opens a gtk window. The gtk works just fine and does what it's supposed to. However, I want to add a thread that will check whether or now the external program is running and if it's finished, automatically destroy the gtk window and exit the script. However, to get the gtk window I obviously have to call gtk.main, which prevents me from launching my own loop testing the other program.
incidentally, my test is based on
child = os.popen("ExternalProgram")
....
....
....
if child.poll() == None:
.....
And I'm not even sure that's the correct way to test (haven't been able to even start testing yet) so perhaps if someone could give me a heads up on that, too
Any idea?
TIA
Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Cooler Master N300 chassis, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703
/ is the root of all problems.
Offline
Just a quick thought: How about reversing the dependancy? Instead, make the GTK application check to see if it's ready to quit. Then, when it quits, your original application will continue running.
Edit: In before Lunar shows up and tells you how to REALLY do it.
Last edited by drcouzelis (2012-05-26 17:05:59)
Offline
Here I am
"os.popen" and related functions have been superseded by the "subprocess" module. Use "subprocess.Popen" to spawn processes.
Wrap the invocation of the background task in a separate class – let's call it "BackgroundWorker" for now – that spawns a background thread which in turn executes the process via "subprocess.call()" and emits a "finished" signal when the process terminated. Create an instance of this class in In "MyWindow.__init__()" (where "MyWindow" is derived from "Gtk.Window") and connect the "BackgroundWorker.finished" signal to a callback which closes the window. In the entry point of your script create an instance of "MyWindow", show it and start the Gtk main loop.
Before that, read the documentation of Gtk and look for a class that wraps background processes, like "QProcess" does in Qt. I don't know if such a class exists in Gtk but if it does it'll significantly ease working with subprocesses in a GUI.
Last but not least a general and very important note concerning threads and GUIs: Do never access any GUI element directly from background thread!
Last edited by lunar (2012-05-26 17:11:25)
Offline
Why use a thread? How about a simple periodic timer with a callback. It all happens in the same thread.
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
@ewaller: Polling kills power-saving. If you want your laptop battery to last, avoid it
Offline
Thank you. Gives me something to work with, though I have to admit that the documentation I've seen thus far isn't the most enlightening, but it's more that I had before. Not marking solved just yet - but I'll be sure to report back
Ryzen 5900X 12 core/24 thread - RTX 3090 FE 24 Gb, Asus B550-F Gaming MB, 128Gb Corsair DDR4, Cooler Master N300 chassis, 5 HD (2 NvME PCI, 4SSD) + 1 x optical.
Linux user #545703
/ is the root of all problems.
Offline