当前位置:   article > 正文

python多线程实现多任务_python多线程实现多任务 mul

python多线程实现多任务 mul

1.简单的线程

同时运行下面的sing和dance函数

  1. import time
  2. import threading
  3. def sing():
  4. for i in range(2):
  5. print("唱歌...")
  6. time.sleep(1)
  7. def dance():
  8. for i in range(2):
  9. print("跳舞...")
  10. time.sleep(1)
  11. if __name__ == '__main__':
  12. sing_process = threading.Thread(target=sing)
  13. dance_process = threading.Thread(target=dance)
  14. sing_process.start()
  15. dance_process.start()

结果如下:

二 、带有参数的线程

  1. import time
  2. import threading
  3. def sing(num):
  4. for i in range(num):
  5. print("唱歌...")
  6. time.sleep(1)
  7. def dance(num):
  8. for i in range(num):
  9. print("跳舞...")
  10. time.sleep(1)
  11. if __name__ == '__main__':
  12. sing_process = threading.Thread(target=sing,args=(2,))
  13. dance_process = threading.Thread(target=dance,kwargs={"num":2})
  14. sing_process.start()
  15. dance_process.start()

一样的结果

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/151655?site
推荐阅读
相关标签
  

闽ICP备14008679号