python中的多线程threading之添加线程:Thread()

版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/PoGeN1/article/details/84035455

百度百科:多线程

多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理(Chip-level multithreading)或同时多线程(Simultaneous multithreading)处理器。 [1] 在一个程序中,这些独立运行的程序片段叫作“线程”(Thread),利用它编程的概念就叫作“多线程处理(Multithreading)”。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程(台湾译作“执行绪”),进而提升整体处理性能。

python中的多线程

多线程是加速程序计算的有效方式,Python的多线程模块threading上手快速简单。
首先要导入多线程模块:threading

import threading

获取已经激活的线程数:

threading.active_count()

查看所有线程信息:

threading.enumerate()

查看正在运行的线程:

threading.current_thread()

添加线程:

threading.Thread()#接收参数target代表这个线程要完成的任务,需自行定义

附上简单添加线程的例子:

import threading

def thread_job():#自行定义的任务
    print("this is an added thread , number is %s" % threading.current_thread())
def main():
    added_thread=threading.Thread(target=thread_job())#target=你要定义的任务
    added_thread.start()#定义完了一个线程要start一下,不然不会启动

if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/PoGeN1/article/details/84035455