前言
很久之前在EAST算法里面见到了多线程,一直没太注意,现在想利用零碎时间学习一下,现在进入我们的第一课。
初识
多线程就是多个任务分给多个人做,这里让我们看几个用到的函数:
import threading
import time
from queue import Queue
def thread_job():
print('这是一个添加线程,名字是%s'%threading.current_thread())
def main():
added_thread=threading.Thread(target=thread_job)#添加一个线程
added_thread.start()#开始线程
print(threading.active_count())#几个线程正在运行
print(threading.enumerate())#分别是是什么
print(threading.current_thread())#运行这个程序用的哪个线程
if __name__=='__main__':
main()
让我们看一下结果:
这是一个添加线程,名字是<Thread(Thread-10, started 5340)>6
[<_MainThread(MainThread, started 12140)>, <Thread(Thread-4, started daemon 4136)>, <Heartbeat(Thread-5, started daemon 12560)>, <HistorySavingThread(IPythonHistorySavingThread, started 6392)>, <ParentPollerWindows(Thread-3, started daemon 1840)>]
<_MainThread(MainThread, started 12140)>