'''
多线程:
1. 通过多线程并行两个函数,两函数分别不停的打印当前时间,
2. 通过打印的时间判断两函数是否并行运行
'''
import threading,time
def process_1(interval_time):
while True :
time.sleep(interval_time) #因为是while循环,所以通过time.sleep()来控制循环间隔,单位S。
print('{}: 进程1'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())))
def process_2(interval_time):
while True :
time.sleep(interval_time)
print('{}: 进程2'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())))
if __name__ == "__main__":
#---------创建线程对象----
threading_1 = threading.Thread(target=process_1, args=(0.5,)) #args内是传递给函数的参数,必须是可迭代对象,通常使用元组
threading_2 = threading.Thread(target=process_2, args=(0.5,))
#--------开启线程-----
threading_1.start()
threading_2.start()
Python3 多线程示例
最新推荐文章于 2024-04-18 18:00:00 发布