Python多執行緒(1)新增執行緒

2021-10-01 12:22:41 字數 1021 閱讀 9009

基本指令&&新增執行緒

import threading

defthread_job()

:print

('this is an added thread,number is %s\n'

% threading.current_thread())

defmain()

: added_threading = threading.thread(target=thread_job)

#建立了乙個執行緒,target=這個執行緒是用來做什麼的

added_threading.start(

)#建立完之後需要start,執行這個執行緒

print

(threading.active_count())

#顯示當前的執行緒數

print

(threading.

enumerate()

)#看一下是哪幾個執行緒

print

(threading.current_thread())

#看一下現在程式執行的哪乙個執行緒

if __name__ ==

'__main__'

: main(

)

執行結果

this is an added thread,number is

9, started 28508

)>5[

<_mainthread(mainthread, started 31764

)>

,4, started daemon 31856

)>

,5, started daemon 10208

)>

,)>

,9, started 28508

)>

]<_mainthread(mainthread, started 31764

)>

Python多執行緒開發1 新增執行緒

匯入執行緒模組 import threading獲取已啟用的執行緒數 threading.active count 2檢視所有執行緒資訊 threading.enumerate mainthread mainthread,started 140736011932608 started daemon ...

多執行緒 新增執行緒Thread

今天我們來學習threading模組的一些基本操作,如獲取執行緒數,新增執行緒等。首先別忘了匯入模組 import threading獲取已啟用的執行緒數 import threading defmain print threading.active count if name main main ...

Python 多執行緒 1

import thread import time 為執行緒定義乙個函式 defprint time threadname,delay count 0while count 3 time.sleep delay count 1print threadname,time.ctime 建立兩個執行緒 t...