python中的多线程而不使用扩展线程类

时间:2018-08-31 12:40:17

标签: python python-3.x

import time
import threading
class test:

    def printer():
        for  i in range(3):
            print('Hello')
            time.sleep(5)
    def reload():
        for i in range(3):
            print('hey')
    def upload():
        print('hii')



if __name__ == '__main__':
    thread1 = threading.Thread(target=printer())
    thread2 = threading.Thread(target=reload())
    thread3 = threading.Thread(target=upload())
    thread1.start()
    thread3.start()
    thread2.start()
    thread1.join()
    thread2.join()
    thread3.join()
    print("bye")
    print("right")
def doc():
    for  i in range(3):
        print('good')

输出:

hello
hello
hello
hey
hey
hey
hii
bye
right

我要在thread1thread2thread3结束后打印def doc。...“ bye”和“ right”由主线程打印,但是doc()没有。 t打印。为什么不打印?

1 个答案:

答案 0 :(得分:0)

def doc():仅定义doc方法。如果要打印其结果,则需要对其进行调用。我建议您将那个块移动到if语句之前,然后在您的doc()语句的最后一行添加一个if调用。因此,If块的最后几行如下所示:

print(“bye”)
print(“right”)
doc()