从从QtThread派生的python进程打开QtApplication

2024-04-18 21:23:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个服务器客户端架构。服务器是QtApplication并且包含QThread。你知道吗

我试图从QThread使用python内置的multiprocessing打开一个新的客户机进程,然后从这个新进程打开一个新的QtApplication。这样服务器和客户机都在运行应用程序

问题是我遇到以下错误:

WARNING: QApplication was not created in the main() thread.

QApplication是在新进程的主线程中创建的,所以我不知道为什么会发生这个错误。你知道吗


Tags: 服务器应用程序客户端客户机进程架构错误not
1条回答
网友
1楼 · 发布于 2024-04-18 21:23:39

科维德·戈亚尔的作品:

Don't use multiprocessing. multiprocessing is not thread safe, on unix it uses fork() without exec() which means that it inherits everything from the parent process including locks (which are in an invalid state in the child process), file handles, global objects like QApplication and so on. Just as an illustration of the problems multiprocessing can cause, if you use it with the standard library logging module you can have your worker processes crash, since the logging module uses locks.

After all, what do you expect to happen to QApplication on fork()? There's no way for fork() to have the QApplication object magically re-initialize itself.

Using multiprocessing will bite you in the rear on any project of moderate complexity. Heck it bit me on the rear even while implementing a muti-core replacment for grep. Instead use subprocess to launch a worker process, feed it a module name, functions and arguments on stdin using cPickle or json and then have it run the task.

见:http://python.6.x6.nabble.com/multiprocessing-with-QApplication-td4977972.html

相关问题 更多 >