python開(kāi)發(fā)之thread線程基礎(chǔ)實(shí)例入門(mén)
本文實(shí)例講述了python開(kāi)發(fā)之thread線程基礎(chǔ)。分享給大家供大家參考,具體如下:
說(shuō)到線程,我們要知道啥是串行,啥是并行程序
舉個(gè)例子:
串行程序,就是一個(gè)一個(gè)的執(zhí)行程序
#python threading
import time
'''
每一秒中,輸出:this is a demo!
'''
def serial():
'''串行輸出'''
time.sleep(1)
print('this is a demo!')
def main():
for i in range(5):
serial()
if __name__ == '__main__':
main()
運(yùn)行結(jié)果如下:
>>> this is a demo! this is a demo! this is a demo! this is a demo! this is a demo! >>>
并行程序,就是很多個(gè)程序在同一時(shí)間(宏觀)一起執(zhí)行
#python threading
import threading
import time
'''
并行執(zhí)行,輸出:Good!Good!Good!Good!Good!
'''
def parallel():
'''并行輸出'''
time.sleep(1)
print('Good!')
def main():
for i in range(5):
t = threading.Thread(target=parallel)
t.start()
if __name__ == '__main__':
main()
當(dāng)然我們通過(guò)執(zhí)行程序,可以知道,并行程序要比串行程序執(zhí)行的要快....
我們也可以獲取到當(dāng)前的線程及個(gè)數(shù):
#python threading
import threading
import time
'''
并行執(zhí)行,輸出:
[<Thread(Thread-2, started 3480)>, <Thread(Thread-1, started 660)>,
<Thread(SockThread, started daemon 2920)>, <Thread(Thread-3, started 916)>,
<Thread(Thread-4, started 3476)>, <_MainThread(MainThread, started 3964)>,
<Thread(Thread-5, started 2060)>]
存在的線程數(shù) : 7
Good!Good!Good!Good!Good!
'''
def parallel():
'''并行輸出'''
time.sleep(1)
print('Good!')
def main():
for i in range(5):
t = threading.Thread(target=parallel)
t.start()
if __name__ == '__main__':
main()
print(threading.enumerate())
print('存在的線程數(shù) : %d'%threading.active_count())
運(yùn)行結(jié)果如下:
>>> [<Thread(SockThread, started daemon 15424)>, <Thread(Thread-3, started 15840)>, <Thread(Thread-1, started 10884)>, <Thread(Thread-2, started 14512)>, <Thread(Thread-4, started 13204)>, <_MainThread(MainThread, started 12924)>, <Thread(Thread-5, started 15476)>] 存在的線程數(shù) : 7 >>> Good!Good!Good!Good!Good!
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python基于Tkinter的HelloWorld入門(mén)實(shí)例
- 安裝Python的web.py框架并從hello world開(kāi)始編程
- python基礎(chǔ)教程之Hello World!
- 從零學(xué)Python之hello world
- Python 第一步 hello world
- Python語(yǔ)法快速入門(mén)指南
- Python入門(mén)學(xué)習(xí)之字符串與比較運(yùn)算符
- Python簡(jiǎn)明入門(mén)教程
- 給Python入門(mén)者的一些編程建議
- python繪圖方法實(shí)例入門(mén)
- Python編程入門(mén)之Hello World的三種實(shí)現(xiàn)方式
相關(guān)文章
利用Chatgpt開(kāi)發(fā)一款加減乘除計(jì)算器(Python代碼實(shí)現(xiàn))
這篇文章主要為大家詳細(xì)介紹了如何利用Chatgpt開(kāi)發(fā)一款加減乘除計(jì)算器(用Python代碼實(shí)現(xiàn)),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02
Python Flask 和 Django 的區(qū)別與適用場(chǎng)景示例分析
Flask和Django是兩個(gè)流行的Python Web框架,但設(shè)計(jì)哲學(xué)、功能和用法有很大區(qū)別,Flask是一個(gè)輕量級(jí)框架,簡(jiǎn)單靈活,適合小型項(xiàng)目和快速原型開(kāi)發(fā),本文給大家介紹Python Flask 和 Django 的區(qū)別與適用場(chǎng)景示例分析,感興趣的朋友跟隨小編一起看看吧2024-10-10
Python中的并發(fā)處理之a(chǎn)syncio包使用的詳解
本篇文章主要介紹了Python中的并發(fā)處理之a(chǎn)syncio包使用的詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Django 對(duì)象關(guān)系映射(ORM)源碼詳解
這篇文章主要介紹了Django 對(duì)象關(guān)系映射(ORM)源碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
PyTorch 編寫(xiě)代碼遇到的問(wèn)題及解決方案
這篇文章主要介紹了PyTorch 編寫(xiě)代碼遇到的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-06-06
pytorch tensor int型除法出現(xiàn)的問(wèn)題
這篇文章主要介紹了pytorch tensor int型除法出現(xiàn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
django 多對(duì)多表的創(chuàng)建和插入代碼實(shí)現(xiàn)
這篇文章主要介紹了django-多對(duì)多表的創(chuàng)建和插入代碼實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09

