python threading模块

threading模块

threading简单介绍

"""
threading 是线程模块 可以使你的程序加快运行速度
使你多线程运行
如果不用这么模块
你的程序使一步一步的运行
用了这个模块你的程序
比如两个print
a = print("1")
b = print("b")
使用前是
1
b
使用后可能是
1b
因为本来你的程序是一步一步的运行
这个模块使你的程序一块运行 但相应你的速度快了你的cpu也处理的多了,如果设置太多会卡
"""

案例一

import threading

def a(n):
    for i in range(1000):
        print("foos%s"%n)

def b(n):
    for i in range(1000):
        print("bar%s"%n)


t1 = threading.Thread(target=a, args=(1,)) # 创建线程对象
t2 = threading.Thread(target=b, args=(2,)) # 创建线程对象

t1.start() # 启动线程
t2.start() # 启动线程

猜你喜欢

转载自blog.csdn.net/weixin_43854835/article/details/89712982