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_Function_Class - Fatal编程技术网

多线程类(python)

多线程类(python),python,multithreading,function,class,Python,Multithreading,Function,Class,如果我有一个类得到了threading.Thread,我就用.start运行新线程 这是一个线程,但当我想在一个类中包含两个线程函数时?我该怎么做 因为当您使用.start时,它会在新线程中使用run函数。请改用线程构造函数的target属性: class twothreads: def t1(self): print "Hi" def t2(self): print "Bye" t = twothreads() threading.Threa

如果我有一个类得到了threading.Thread,我就用.start运行新线程

这是一个线程,但当我想在一个类中包含两个线程函数时?我该怎么做

因为当您使用.start时,它会在新线程中使用run函数。

请改用线程构造函数的target属性:

class twothreads:
    def t1(self):
        print "Hi"

    def t2(self):
        print "Bye"

t = twothreads()
threading.Thread(target=t.t1).start()
threading.Thread(target=t.t2).start()
改为使用线程构造函数的target属性:

class twothreads:
    def t1(self):
        print "Hi"

    def t2(self):
        print "Bye"

t = twothreads()
threading.Thread(target=t.t1).start()
threading.Thread(target=t.t2).start()

谢谢,我不知道它可以那样用谢谢,我不知道它可以那样用