【问题标题】:change to main thread, from another thread in python [duplicate]从python中的另一个线程更改为主线程[重复]
【发布时间】:2020-08-22 06:18:18
【问题描述】:

有没有办法从后台线程转移到主线程,例如;

import threading

def func(txt):
    if txt == 'foo':
        ## must do the processing in the main thread
        pass

    else:
        print(txt)


thread = threading.Thread(target = func, args = ('hello',))
thread.start()

提前致谢:)

【问题讨论】:

    标签: python python-3.x multithreading pyqt5


    【解决方案1】:

    您可以在主线程中有一个变量,该变量将在 func 中更改。然后在 main 中检查变量的状态。

    如果是 foo,那么做点什么。

    【讨论】:

    • 是的,好吧,想象一下在 Thread "thread" 中有一个无限循环,即使你在 Thread "thread" 中调用其他函数,它也会在 Thread "thread" 中执行,而不是在“main_thread”,我需要一种方法来完全改成主线程。
    • 抱歉误会了我。在主线程中设置一个您在线程中更改的变量。为此可能需要全局变量名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-06
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多