zoukankan      html  css  js  c++  java
  • python线程进程

    关于python线程的用法有很多,也很详细,但是我就想实现两个while1同时执行,却废了很长的时间,直接进入正题

    import time
    def print_111():
        while 1:
            print('1111')
            time.sleep(1)
    def print_222():
        while 1:
            print('222')
            time.sleep(1)
    

      

    上面两个函数,都是while 1循环,分别定时打印‘111’和‘222’,为了让他们同时打印,需要引入线程。下面是全部代码

    import threading
    import time
    
    def print_111():
        while 1:
            print('1111')
            time.sleep(1)
    def print_222():
        while 1:
            print('222')
            time.sleep(1)
    
    threads = []
    
    t1 = threading.Thread(target=print_111)
    threads.append(t1)
    t2 = threading.Thread(target=print_222)
    threads.append(t2)
    if __name__=='__main__':
        for t in threads:
            t.start()
        for t in threads:
            t.join()
    print ("退出线程")
    

      

    版权声明:本文为CSDN博主「英俊幽默又有才」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

    原文链接:https://blog.csdn.net/qq_34824856/article/details/80939449

  • 相关阅读:
    五子棋
    团队项目:五子棋
    101空降师506团2营E连全体成员
    团队作业七
    作业六
    团队作业(五)——旅游行业的手机App
    团队任务四(无图)
    团队作业三(补二)
    菜的抠脚团队正式成立
    团队作业七
  • 原文地址:https://www.cnblogs.com/chargeworld/p/12245600.html
Copyright © 2011-2022 走看看