python - 多线程类(python)

标签 python multithreading function class

如果我有一个获得 threading.Thread 的类,我会使用 .start()

运行新线程
class hello(threading.Thread):
    def run():
        print "hi"
        print "bye"

所以这是一个线程,但是当我想在一个类中使用 2 个线程函数时?我该怎么做?

因为当你使用 .start() 它会在新线程中使用 run 函数。

最佳答案

使用 target 属性代替 Thread 构造函数:

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()

关于python - 多线程类(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18422131/

相关文章:

c - 数列中的第 n 个元素,其中数字可被其数字之和整除

python - 如何在python中将azure自定义视觉注释格式更改为yolo v8格式

python - 基于最小/最大值的 Numpy 动态数组切片

python - 记录数据并同时检查条件的最有效方法?

function - Matlab 中带有函数名的变量的警告

c - 将字符串数组从函数传递到主函数(C 代码)

python - 简单的Python程序-卡住了

python - 线程和多处理组合 [Python]

java - 我需要在 synchronized(anyObject) block 中使用 AtomicInteger 吗?

multithreading - iOS4 & 背景 [UIImage setImage :]