Python多线程发送一个函数从子线程在主线程中运行并等待它直到完成

Fen*_*Zhu 6 python multithreading pyqt

我有一个关于 python 中多线程的问题。基本上在我的软件中,我得到了一个运行成本很高的功能。因此,我把它放在子线程中运行。但是,在函数运行过程中,它会调用一个函数并更改对象的某些值,从而触发GUI 更改。因此,我需要将其放回主线程。

问题是我需要等待这个函数在主线程中完全完成,然后在子线程中继续执行该函数。

因此,我需要一种巧妙的方法将作业发送回主线程并等待它完成。有人可以给我任何建议吗?

非常感谢。我在下面添加了我的运行线程方法

此致,

import Queue
import logging
import sys
import time
import threading


DISABLE_THREADING = False

queue = Queue.Queue()


def run_in_sub_thread_named(name, function, *params):

    if DISABLE_THREADING:
        return function(*params)
    th = _DEThread(function, *params)
    task_info = TaskInfo(name)
    th.task_info = task_info
    running_tasks[th] = task_info
    th.start()
    return th

def run_in_main_thread(function, *params, **kwargs):
    cur_thread = threading.currentThread()
    cur_thread_name = cur_thread.getName()
    in_main_thread = (cur_thread_name == 'MainThread')
    if in_main_thread:
        function(*params, **kwargs)
    else:
        queue.put((function, params, kwargs))



class _DEThread:
    def __init__(self, function, *params):
        self.thread = threading.Thread(target=self.execute)
        self.thread.setDaemon(True)
        self.function = function
        self.params = params
        self.name = ''

    def start(self):
        self.thread.start()

    def execute(self):
        try:
            if self.params is ():
                res = self.function()
            else:
                res = self.function(*self.params)
        except:
            run_in_main_thread(self._update_task_info,
                'Exception: %s' % repr(sys.exc_info()))
            raise
        run_in_main_thread(self._update_task_info, res)

    def _update_task_info(self, res):
        if self in running_tasks:
            task_info = running_tasks[self]
            task_info.end_time = time.time()
            task_info.result = res
            finished_tasks[self] = task_info
            del running_tasks[self]
Run Code Online (Sandbox Code Playgroud)

Bre*_*bel 5

通常,Qt 中线程之间调用函数的方式是使用信号和插槽。也可以使用QMetaObject.invokeMethod调用另一个线程中的函数,并使用Qt.BlockingQueuedConnection类型强制它阻塞线程,直到主线程函数完成。不过,您将需要使用QThreads而不是 python threads。查看文档中描述的Worker模式QThread,了解如何重构您的代码以使用QThreads

def function_in_thread(self):
    # This will block the thread until function completes
    # in main thread.
    QtCore.QMetaObject.invokeMethod(self.main_gui, 'nameOfMethod', QtCore.Qt.BlockingQueuedConnection)
Run Code Online (Sandbox Code Playgroud)