首页 > 解决方案 > 如何使用python线程

问题描述

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

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

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

提前致谢。

标签: pythonpython-3.xpython-module

解决方案


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

thread1 = threading.Thread(target=procedure1)

推荐阅读