Connect Qprocess to already running program

Heads up! You've already completed this tutorial.

mrwonderfulness11014 | 2021-04-30 16:26:44 UTC | #1

Here's a trick question...

I followed your tutorial about using Qprocess to launch an external script/program. This allows me to start the program FROM a pyqt application and monitor and get results from it....but what if I need to connect to an exterbal program that is ALREADY running in the background 24/7 ? Is there a way to code that program so that it is outputting stuff all the time....and then when i connect with a pyqt gui it gives me a view on it? Thanks.

mrwonderfulness11014 | 2021-05-02 22:44:08 UTC | #2

i guess this is not possible? someone with experience with this could you please write a "yes" or "no"? or direct me to a place to find out more? thank you!

martin | 2021-05-05 08:03:55 UTC | #3

Hi @mrwonderfulness11014 welcome to the forum!

Which platform is your process running on, and how is it started? Do you have control over the launching of the background process?

The easiest way is to redirect output to a file when starting up your long running process. If you only want to get results from it then this would be enough.

On Linux if you start a process using screen you can attach/detach it will -- it should be possible to launch screen from within a QProcess and from there communicate as normal. Alternatively, if you know the pid of the running process you can also (with the same user) read from the pseudo-files at /proc/<pid>/fd (0 stdin, 1 stdout, 2 stderr). Lastly, you can connect to a running processing using gdb.

mrwonderfulness11014 | 2021-05-08 03:07:17 UTC | #4

Hi Martin, thank you for your reply. yes, after googling around and looking at qt docs this seems to be the case. for working with python, what would you say the most performant file reading setup could be? i'll have quite a bit of data. perhaps something like redis or other in memory db, sqlite, etc. i'll run some tests but i'm wondering if there is a best practices way of doing this with python and qt. best regards.

martin | 2021-05-10 08:03:29 UTC | #5

It's difficult to give a definitive answer without seeing the data. If you need to read the data repeatedly, in parts or lookup specific elements then sqlite or a database (in memory/on disk) would be better than files. But if you're just loading different datasets and can't prepare a database/memory store in advance, files would be the better option.

Qt doesn't really mind how you have the data stored. If you're using the model views you'll need to implement an interface (and some kind of caching) since the views require a lot of data lookups for individual cells (usually very inefficient to hit a database for that).

If you're passing data in/out of a process then an external data store then you have a choice between piping the data or using an external data store (including a a file). Pipes make more sense when streaming the data (i.e. you have a live output) and don't need to store the result. If you're storing the data and only need access to the finished result, I'd put it in a store/file and just notify the main process to read it once you're done.

Does that make sense? If you have more details of the data I'm happy to go into it further.

mike2750 | 2021-06-07 12:53:24 UTC | #6

here is an example with tmux i found helpful way back.

what martin mentioned about writing to a file and then using something like this would be one way depending on the app and how its outputting information.

Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt5
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Connect Qprocess to already running program was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.