Unsolved Can QProcess runs eventloop without starting external program?
-
I want to write a multi-process program and I want to make parent process as works dispatcher and make all child processes as worker. Parent process dispatches works with channel. Can Qt possible to implement?
I check out the documentation from : http://doc.qt.io/qt-5/qprocess.html, and found that all
QProcess::start()
functions are requiredprogramName
is set. -
Hi! Yes that's possible, no problem. QProcess offers an asynchronous API for talking to the child process it manages. You can have as many child processes as you wish. You need one QProcess object per child process.
-
@Wieland
I want to do like this
pid_t pid = fork(); if (pid == 0) { // child process do work } else { // parent process do work }
However, QProcess looks like only provide API to start external program. Is any way to execute code but not a program?
-
@FloatFlower.Huang To me your use case sounds more like something for multithreading. Why do you want to use processes?
"execute code but not a program" - a process always executes a program. -
Oh, I see. No, you can't use QProcess here and there's no other Qt API for this. Just take the usual POSIX / Windows stuff (fork, clone, pipes, etc).
-
@FloatFlower.Huang
Qt concerns itself mainly with cross-platform solutions. What you want to do is highly Linux-only: try implementingfork()
under Windows! It doesn't work like that.QProcess
is about spawning a separate sub-process. As @jsulm says, surely you want to use Qt threading here? -
@jsulm @JNBarchan
I am going to implement multi-process and each process manage many threads just like Nginx HTTP server. In Nginx HTTP server, the master process fork multiple worker threads and each process have many threads to handle requests. Because there is limit amount of threads per process, so I am thinking about how to increase the thread amount in the program.
-
@FloatFlower.Huang Well, you can use QProcess for that. You can even start same app using it, just pass parameters to it tell it what it should do.
-
@FloatFlower.Huang said in Can QProcess runs eventloop without starting external program?:
Because there is limit amount of threads per process, so I am thinking about how to increase the thread amount in the program.
Don't do this! Just make sure not to exceed this limit. If you reach the limit queue other jobs and take them from the queue as soon as a thread is free. Threads and even more so processes consume ressources like memory, CPU and operating system resources. It does not make sense to have too many threads because the overhead to manage them will become too high and you will not get any performance improvements anymore.