------------恢复内容开始------------
参考视频:莫烦python https://mofanpy.com/tutorials/python-basic/threading/why/
# -*- coding: utf-8 -*- import threading def main(): print(threading.active_count()) #运行的线程个数 print(threading.enumerate()) #运行的是哪些线程 print(threading.current_thread()) #当下运行的是哪个线程 if __name__ == ‘main‘: main()
6
[<_MainThread(MainThread, started 13824)>, <Thread(Thread-4, started daemon 1452)>, <Heartbeat(Thread-5, started daemon 14768)>, <HistorySavingThread(IPythonHistorySavingThread, started 4796)>, <ParentPollerWindows(Thread-3, started daemon 1852)>, <GarbageCollectorThread(Thread-6, started daemon 992)>]
<_MainThread(MainThread, started 13824)>
# -*- coding: utf-8 -*- import threading def thread_job(): print(‘This is an added Theard,number is %s‘ % threading.current_thread()) def main(): added_thread = threading.Thread(target=thread_job) added_thread.start() if __name__ == ‘main‘: main()
This is an added Theard,number is <Thread(Thread-10, started 8780)>
# -*- coding: utf-8 -*- import threading import time def thread_job(): print("T1 start\n") for i in range(10): time.sleep(0.1) print("T1 finish\n") def main(): added_thread = threading.Thread(target=thread_job,name="T1") added_thread.start() print("all done\n") if __name__ == ‘main‘: main()
T1 start
all done
T1 finish
可以看出,all done 在 T1 finish 之前,多个线程是同时执行的。要想让 all done 输出在 T1 finish 后面,可以用 joint。
# -*- coding: utf-8 -*- import threading import time def thread_job(): print("T1 start\n") for i in range(10): time.sleep(0.1) print("T1 finish\n") def main(): added_thread = threading.Thread(target=thread_job,name="T1") added_thread.start() added_thread.join() print("all done\n") if __name__ == ‘main‘: main()
T1 start
T1 finish
all done
再一个例子
# -*- coding: utf-8 -*-
import threading
import ti
def thread_job(): print("T1 start\n") for i in range(10): time.sleep(0.1) print("T1 finish\n") def thread_job2(): print("T2 start\n") print("T2 finish\n") def main(): added_thread = threading.Thread(target=thread_job,name="T1") added_thread2 = threading.Thread(target=thread_job2,name="T2") added_thread.start() added_thread2.start() #added_thread.join()
#added_thread2.join()
print("all done\n") if __name__ == ‘main‘: main()
T1 start
T2 start
T2 finish
all done
T1 finish
有了 added_thread.join() 以后,输出变为
T1 start
T2 start
T2 finish
T1 finish
all done
用于装多线程的运算结果
# -*- coding: utf-8 -*- import threading from queue import Queue def job(l,q): for i in range(len(l)): l[i] = l[i]**2 q.put(l) #将计算结果放入q,不能用return def multiThreading(): q = Queue() threads=[] #装四个线程 data = [[1,2,3],[4,5,6],[6,6,6],[8,8,8]] for i in range(4):#分别运行四个线程并传入参数 t = threading.Thread(target=job,args=(data[i],q)) t.start() threads.append(t) for thread in threads: thread.join() results=[] #将结果一个个取出到 results 并输出 for _ in range(4): results.append(q.get()) print(results) if __name__=="__main__": multiThreading()
[[1, 4, 9], [16, 25, 36], [36, 36, 36], [64, 64, 64]]
------------恢复内容结束------------
原文:https://www.cnblogs.com/caiyishuai/p/15188979.html