当前位置: 移动技术网 > IT编程>脚本编程>Python > python线程的几种创建方式

python线程的几种创建方式

2019年08月24日  | 移动技术网IT编程  | 我要评论

白发弃妾,国产车哪个最好,原点数码

python3 线程中常用的两个模块为:

  • _thread
  • threading(推荐使用)

    使用thread类创建

import threading
from time import sleep,ctime

def sing():
    for i in range(3):
        print("正在唱歌...%d"%i)
        sleep(1)

def dance():
    for i in range(3):
        print("正在跳舞...%d"%i)
        sleep(1)

if __name__ == '__main__':
    print('---开始---:%s'%ctime())

    t1 = threading.thread(target=sing)
    t2 = threading.thread(target=dance)

    t1.start()
    t2.start()

    #sleep(5) # 屏蔽此行代码,试试看,程序是否会立马结束?
    print('---结束---:%s'%ctime())
"""
输出结果:
---开始---:sat aug 24 08:44:21 2019
正在唱歌...0
正在跳舞...0---结束---:sat aug 24 08:44:21 2019
正在唱歌...1
正在跳舞...1
正在唱歌...2
正在跳舞...2
"""

说明:主线程会等待所有的子线程结束后才结束

使用thread子类创建

为了让每个线程的封装性更完美,所以使用threading模块时,往往会定义一个新的子类class,只要继承threading.thread就可以了,然后重写run方法。

import threading
import time

class mythread(threading.thread):
    def run(self):
        for i in range(3):
            time.sleep(1)
            msg = "i'm "+self.name+' @ '+str(i) #name属性中保存的是当前线程的名字
            print(msg)


if __name__ == '__main__':
    t = mythread()
    t.start()
"""
输出结果:
i'm thread-5 @ 0
i'm thread-5 @ 1
i'm thread-5 @ 2
"""

使用线程池threadpoolexecutor创建

from concurrent.futures import threadpoolexecutor
import time
import os


def sayhello(a):
    for i in range(10):
        time.sleep(1)
        print("hello: " + a)


def main():
    seed = ["a", "b", "c"]
    # 最大线程数为3,使用with可以自动关闭线程池,简化操作
    with threadpoolexecutor(3) as executor:
        for each in seed: 
            # map可以保证输出的顺序, submit输出的顺序是乱的
            executor.submit(sayhello, each)

    print("主线程结束")


if __name__ == '__main__':
    main()

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网