从 class 传递函数而不启动它 python

pass function from class without starting it python

我需要运行两个函数与threading模块

你可以做到这一点Thread(target=my_func)

但我想要 class 中的函数(导入 class)所以我尝试了 Thread(target=a_class.my_func)

但是没有用,然后我试了Thread(target=a_class.my_func())

但是这个函数开始 运行 函数因为我调用了它,这是一个无限循环所以下一个函数永远不会 运行.

我该怎么做?

您应该包装 class 方法,如下所示:

import threading

def my_func(self):
    a_class.my_func()

def my_other_func(self):
    a_class.my_other_func()

t1 = threading.Thread(target=my_func)
t2 = threading.Thread(target=my_other_func)