Python線程之多線程展示詳解
什么多線程?
多線程,就是多個(gè)獨(dú)立的運(yùn)行單位,同時(shí)執(zhí)行同樣的事情。
想想一下,文章發(fā)布后同時(shí)被很多讀者閱讀,這些讀者在做的事情‘閱讀'就是一個(gè)一個(gè)的線程。
多線程就是多個(gè)讀者同時(shí)閱讀這篇文章。重點(diǎn)是:同時(shí)有多個(gè)讀者在做閱讀這件事情。
如果是多個(gè)讀者,分時(shí)間閱讀,最后任意時(shí)刻只有一個(gè)讀者在閱讀,雖然是多個(gè)讀者,但還是單線程。
我們再拿前面分享的代碼:關(guān)注和點(diǎn)贊。
def dianzan_guanzhu():
now = datetime.datetime.now()
name = "python萌新"
print("%s name:%s" % (now, name))
time.sleep(1)
result = "好棒!" + name + " 關(guān)注雷學(xué)委,學(xué)會(huì)了開發(fā)知識!"
print("%s result:%s" % (now, result))
return result
我們看看下面的代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/21 12:02 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷學(xué)委
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello
import threading
import datetime
import time
def dianzan_guanzhu():
now = datetime.datetime.now()
name = "python萌新"
print("%s name:%s" % (now, name))
time.sleep(1)
result = "好棒!" + name + " 關(guān)注雷學(xué)委,學(xué)會(huì)了開發(fā)知識!"
print("%s result:%s" % (now, result))
return result
for i in range(3):
mythread = threading.Thread(name="t-" + str(i), target=dianzan_guanzhu)
print("mythread:", mythread)
print("is_alive:", mythread.is_alive())
mythread.start()
print("is_alive:", mythread.is_alive())
Thread類可以傳入name指定線程名字。
直接復(fù)制運(yùn)行,這里我們創(chuàng)建了3個(gè)線程。
它們依次調(diào)用了dianzan_guanzhu函數(shù)
下面是運(yùn)行結(jié)果:

這3個(gè)線程不同時(shí)間打印完成了,但是內(nèi)容打印亂序了,甚至還串行了。
讀者同學(xué)可以多運(yùn)行幾次。
獲取活躍線程相關(guān)數(shù)據(jù)
threading.active_count函數(shù): 可以獲取活躍線程數(shù)。threading.current_thread函數(shù):可以獲取活躍線程對象,這樣我們可以獲取這樣獲取線程名稱:threading.current_thread().getName()。
前文說過了,加上主線程,一共是4個(gè)線程。
運(yùn)行下面代碼看看:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/21 12:02 上午
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: 雷學(xué)委
# @XueWeiTag: CodingDemo
# @File : __init__.py.py
# @Project : hello
import random
import threading
import datetime
import time
def dianzan_guanzhu():
thread_name = threading.current_thread().getName()
now = datetime.datetime.now()
print("線程啟動(dòng)了:", thread_name)
name = "python萌新"+thread_name
print("%s - %s name:%s" % (thread_name, now, name))
time.sleep(1)
result = "好棒!" + name + " 關(guān)注雷學(xué)委,學(xué)會(huì)了開發(fā)知識!"
print("%s - %s result:%s" % (thread_name, now, result))
return result
for i in range(3):
mythread = threading.Thread(name="t-" + str(i), target=dianzan_guanzhu)
print("mythread:", mythread)
print("is_alive:", mythread.is_alive())
mythread.start()
ac = threading.active_count()
print("active_count:", ac)
如果我們把活躍線程數(shù)打印,那么等3個(gè)線程都start調(diào)用了。
加上主線程,最多是4個(gè)活躍線程。

今天先展示一下多個(gè)線程執(zhí)行同個(gè)任務(wù)的代碼實(shí)現(xiàn)。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
python矩陣的轉(zhuǎn)置和逆轉(zhuǎn)實(shí)例
今天小編就為大家分享一篇python矩陣的轉(zhuǎn)置和逆轉(zhuǎn)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python實(shí)現(xiàn)電子產(chǎn)品商店
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)電子產(chǎn)品商店,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
python 基于空間相似度的K-means軌跡聚類的實(shí)現(xiàn)
這篇文章主要介紹了python 基于空間相似度的K-means軌跡聚類的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

