python - 如何使用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()
然而,这使得这两个程序一个接一个地运行,而不是像我要求的那样并行运行。只需要完成此示例即可工作,我们将不胜感激。
提前致谢。
解决方案
您导入threading
但不使用它。尝试:
thread1 = threading.Thread(target=procedure1)
推荐阅读
- javascript - 当sweetalert弹出消息显示时如何使复选框值不改变
- java - Spring验证:MethodArgumentNotValidException:代码存储在哪里?
- python - 用于 Kibana 应用程序的 Okta OpenID Connect
- c# - 如何在html中显示价值?
- asp.net - 如何使用 asp.net 在日历控件中添加日期?
- javascript - 使用 Polyfills/transpiler 会使我们的网站变慢吗?
- mysql - 为什么当我启动 phpmyadmim 时会出现这种文字?
- qt - QML.jl:没有为 QML 找到适用的加载程序
- javascript - 动态添加数据属性到元素
- java - Java Map 按键归约