Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用类构造函数作为函数创建线程_Python_Multithreading_Class_Constructor - Fatal编程技术网

Python 使用类构造函数作为函数创建线程

Python 使用类构造函数作为函数创建线程,python,multithreading,class,constructor,Python,Multithreading,Class,Constructor,我知道我可以使用以下语法在Python中创建和启动线程: import threading tracker_thread = threading.Thread(target=func, args=(x,y,z)) tracker_thread.daemon = True tracker_thread.start() 我需要的是使用类构造函数作为target。我知道解决方案如下: def wrap_function(x,y,z): MyClas(x,y,z) tracker_thre

我知道我可以使用以下语法在Python中创建和启动线程:

import threading

tracker_thread = threading.Thread(target=func, args=(x,y,z))
tracker_thread.daemon = True
tracker_thread.start()
我需要的是使用类构造函数作为
target
。我知道解决方案如下:

def wrap_function(x,y,z):
    MyClas(x,y,z)

tracker_thread = threading.Thread(target=wrap_function, args=(x,y,z))
但是我试图通过在
线程
的初始化中只使用构造函数来避免使用类似的“wrap函数”


我该怎么做?

已解决。最简单的解决方案总是最好的:

tracker_thread = threading.Thread(target=MyClass, args=(x,y,z))

感谢您的评论。

threading.Thread(target=MyClass,args=(x,y,z))
不起作用吗?是的,它起作用了。。。谢谢,是args=[x,y,z]还是args=(x,y,z)?是的——Python“没有用于实例化对象的特殊语法”再次获胜!函数/类交换给我的唯一一次机会是将decorator作为类编写,并使用它在Python3中修饰方法。(提示:您的装饰程序将需要一个
\uuuu获取\uuuu
方法)