首页 > 编程语言 > 详细

python 之 Threading 多线程((更新中)

时间:2021-08-26 13:49:39      阅读:28      评论:0      收藏:0      [点我收藏+]

------------恢复内容开始------------

参考视频:莫烦python https://mofanpy.com/tutorials/python-basic/threading/why/

1.多线程简单介绍

# -*- 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)>

2.添加多线程

# -*- 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)>

3. join 功能

# -*- 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

4. Queue 功能

用于装多线程的运算结果

 

# -*- 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]]

 

 

 

------------恢复内容结束------------

python 之 Threading 多线程((更新中)

原文:https://www.cnblogs.com/caiyishuai/p/15188979.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!