简体   繁体   English

如何使用python线程

[英]How to use python threading

I want to be able to have two procedures run at the same time, current code below;我希望能够同时运行两个程序,当前代码如下;

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

However this makes the two procedures run one after the other rather than parallel as I will require.然而,这使得这两个程序一个接一个地运行,而不是像我需要的那样并行运行。 Just need this example completed to work and will be greatly appreciated.只需要完成此示例即可工作,将不胜感激。

Thanks in advance.提前致谢。

You import threading but don't use it.您导入threading但不使用它。 Try:尝试:

thread1 = threading.Thread(target=procedure1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM