Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. How to use QProcess with QtConcurrent?
Forum Updated to NodeBB v4.3 + New Features

How to use QProcess with QtConcurrent?

Scheduled Pinned Locked Moved Unsolved Qt 6
15 Posts 3 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote last edited by
    #4

    My question was - why do you use a thread here at all? It's not needed.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    T 1 Reply Last reply
    1
    • Christian EhrlicherC Christian Ehrlicher

      My question was - why do you use a thread here at all? It's not needed.

      T Offline
      T Offline
      Teg Miles
      wrote last edited by
      #5

      @Christian-Ehrlicher I thought it would be faster with a thread. Why not?

      Christian EhrlicherC 1 Reply Last reply
      0
      • T Teg Miles

        @Christian-Ehrlicher I thought it would be faster with a thread. Why not?

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote last edited by
        #6

        @Teg-Miles said in How to use QProcess with QtConcurrent?:

        Why not?

        As I said - it's not needed and as you see - it just creates complexity and problems for no reason.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        T 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @Teg-Miles said in How to use QProcess with QtConcurrent?:

          Why not?

          As I said - it's not needed and as you see - it just creates complexity and problems for no reason.

          T Offline
          T Offline
          Teg Miles
          wrote last edited by
          #7

          @Christian-Ehrlicher Is it possible? If yes I just want to know how to.

          Christian EhrlicherC JonBJ 2 Replies Last reply
          0
          • T Teg Miles

            @Christian-Ehrlicher Is it possible? If yes I just want to know how to.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote last edited by
            #8

            @Teg-Miles said in How to use QProcess with QtConcurrent?:

            Is it possible?

            What? Using a QThread or QtConcurrent here? Yes why not? I gave you a hint why it might not work but we can't say more

            what does get_data_output_list()? How do you wait for the process to finish?

            And no, I will not debug this - there is simply not QThread needed here so don't make it more complicated as needed...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • T Teg Miles

              @Christian-Ehrlicher Is it possible? If yes I just want to know how to.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote last edited by
              #9

              @Teg-Miles
              Running multiple concurrent ps commands to parse their output is not a good idea. If you are going to get the information from ps for simplicity (I think it gets most of its information from the /proc filesystem, which you could do yourself and there is probably sample code out there) why not run one periodically, not many at once?

              T 1 Reply Last reply
              0
              • JonBJ JonB

                @Teg-Miles
                Running multiple concurrent ps commands to parse their output is not a good idea. If you are going to get the information from ps for simplicity (I think it gets most of its information from the /proc filesystem, which you could do yourself and there is probably sample code out there) why not run one periodically, not many at once?

                T Offline
                T Offline
                Teg Miles
                wrote last edited by
                #10

                @JonB That's what I'm trying to do. Get info about active processes and updating them in a widget window every second. I can do it all in a main thread but I want to do most of the heavy job in a separate thread. And QProcess can works asynchronously but I don't know how.

                Christian EhrlicherC JonBJ 2 Replies Last reply
                0
                • T Teg Miles

                  @JonB That's what I'm trying to do. Get info about active processes and updating them in a widget window every second. I can do it all in a main thread but I want to do most of the heavy job in a separate thread. And QProcess can works asynchronously but I don't know how.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote last edited by Christian Ehrlicher
                  #11

                  @Teg-Miles said in How to use QProcess with QtConcurrent?:

                  And QProcess can works asynchronously but I don't know how.

                  All explained in the docs: https://doc.qt.io/qt-6/qprocess.html#details and https://doc.qt.io/qt-6/signalsandslots.html

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • T Teg Miles

                    @JonB That's what I'm trying to do. Get info about active processes and updating them in a widget window every second. I can do it all in a main thread but I want to do most of the heavy job in a separate thread. And QProcess can works asynchronously but I don't know how.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote last edited by JonB
                    #12

                    @Teg-Miles

                    • QProcess runs a process "in the background". So long as you do not call waitForFinished() or similar your calling process does not block or wait. So, if you like, it is already running in its own thread (actually process). That is why you absolutely do not need or want to do anything about creating your own threads or any QtConcurrent stuff.

                    • QtConcurrent is used to create multiple, simultaneous threads. But you do not want more than one ps process running at a time. You want a single one to run, come to an end, you parse its output and update your table. Then after a delay (like one second) you want to run a new one and update from that. So you want to use a repeating interval QTimer, nothing concurrent.

                    • As I said earlier, if you want to make it at all efficient rather than just a "play around" you will not want to spawn any ps process, instead you will want to use the right C++ calls to read the information from the /proc filesystem.

                    Please read @Christian-Ehrlicher's links and throw away any QtConcurrent stuff :)

                    T 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @Teg-Miles

                      • QProcess runs a process "in the background". So long as you do not call waitForFinished() or similar your calling process does not block or wait. So, if you like, it is already running in its own thread (actually process). That is why you absolutely do not need or want to do anything about creating your own threads or any QtConcurrent stuff.

                      • QtConcurrent is used to create multiple, simultaneous threads. But you do not want more than one ps process running at a time. You want a single one to run, come to an end, you parse its output and update your table. Then after a delay (like one second) you want to run a new one and update from that. So you want to use a repeating interval QTimer, nothing concurrent.

                      • As I said earlier, if you want to make it at all efficient rather than just a "play around" you will not want to spawn any ps process, instead you will want to use the right C++ calls to read the information from the /proc filesystem.

                      Please read @Christian-Ehrlicher's links and throw away any QtConcurrent stuff :)

                      T Offline
                      T Offline
                      Teg Miles
                      wrote last edited by
                      #13

                      @JonB Thank you for the explanation. I'm understand now that QProcess wasn't created for threading. I thought that ps will be faster than reading from /proc.
                      Is it not the case? Why? And for Windows is there a better way to get active processes than use Get-Process?

                      Christian EhrlicherC JonBJ 2 Replies Last reply
                      0
                      • T Teg Miles

                        @JonB Thank you for the explanation. I'm understand now that QProcess wasn't created for threading. I thought that ps will be faster than reading from /proc.
                        Is it not the case? Why? And for Windows is there a better way to get active processes than use Get-Process?

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote last edited by
                        #14

                        @Teg-Miles said in How to use QProcess with QtConcurrent?:

                        I'm understand now that QProcess wasn't created for threading.

                        This is simply not true. It can be used in a QThread/std::thread/whatever but it's nonsense in your usecase as we already told you several times...

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • T Teg Miles

                          @JonB Thank you for the explanation. I'm understand now that QProcess wasn't created for threading. I thought that ps will be faster than reading from /proc.
                          Is it not the case? Why? And for Windows is there a better way to get active processes than use Get-Process?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote last edited by
                          #15

                          @Teg-Miles
                          It's not so much that QProcess "wasn't created for threading", it's that there is no need (and only added complexity) to use threads to run the the processes since another process is asynchronous anyway. It won't even use any calling threads you might create anyway, as soon as a sub-process runs it is in its own thread/process anyway, not the one which ran it.

                          No, ps will not be faster than, say, reading from /proc yourself as that is what it will be doing anyway --- it's not magic, it has to be written in C/C++ itself anyway. OTOH there is an overhead inherent in creating and running another process, plus whatever IPC or I/O you do to get its data back. That will be true on Windows too. I assume Get-Process is a PowerShell command a bit like ps? So again that is just one way you could call it. There will also be Windows own system calls to get information about other processes, and calling those yourself from C/C++ will get better performance. But finding out how to do this in Linux/Windows/MacOS may be something you don't want to do and you find running some command on each OS and reading its output is what you prefer for a simple, non-commercial program.

                          1 Reply Last reply
                          2

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved