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

posted @ 2020-01-31 15:48  知一以天  阅读(135)  评论(0编辑  收藏  举报