如何使用python线程

How to use python threading

我希望能够同时拥有两个程序运行,当前代码如下;

import time,threading

def procedure1():
    for i in range(0,5):
        time.sleep(1)
        print('hello')

def procedure2():
    for j in range(0,10):
        time.sleep(1)
        print(j)

thread1=procedure1()
thread2=procedure2()

thread1.start()
thread2.start()

然而,这使得两个过程 运行 一个接一个地进行,而不是像我所要求的那样并行进行。只需要完成此示例即可工作,我们将不胜感激。

提前致谢。

您导入 threading 但不使用它。尝试:

thread1 = threading.Thread(target=procedure1)