由於之前看書的時候,對多執行緒的呼叫不怎麼了解,藉此機會,看了一些資料自己整理了一下
osi七層模型:
物理層資料鏈路層
網路層傳輸層
會話層表示層
應用層協議分為tcp 、ip協議
現在用得最多的是這兩種協議,tcp\ip,ip分管各個電腦終端位址,負責發包(包:就是資料)以塊的形式傳送,
tcp負責運輸資料,保證運輸到達不同的ip
至於識別資料是不同ip位址下的埠控制分配給哪個程式使用那部分的資料
socket(family,type[,protocal])
family:套接字家族,af_unix、af_inet、af_inet6
type:套接字型別,可根據面向連線還是非連線分為sock_stream或sock_dgram
protocol:一般不填,預設為0
利用建立多執行緒來列印時間戳
#coding=utf-8
#version:python 3.7.6
#tool:pycharm 4.5.4
__date =
' '__author__ =
'alexin'
import threading
import time
exitflag=
0class
mythread
(threading.thread)
:def
__init__
(self,threadid,name,counter)
: threading.thread.__init__(self)
self.threadid=threadid
self.name=name
self.counter=counter
defrun(self)
:print
("starting"
+self.name)
#獲得鎖
threadlock.acquire(
)print
(self.name,
'獲得鎖'
) print_time(self.name,self.counter,3)
print
(self.name,
"釋放鎖"
) threadlock.release(
)#釋放鎖
print
("exiting"
+self.name)
defprint_time
(threadname,delay,counter)
:while counter:
if exitflag:
thread.exit(
) time.sleep(delay)
print
("%s:%s"
%(threadname,time.ctime())
) counter -=
1#建立乙個指令鎖
threadlock=threading.lock(
)thread=
for i in
range(0
,3):
1)thread[i]
=mythread(i+1,
"thread-"
+str
(i),i+1)
thread[i]
.start(
)#用於主程式直到執行緒完全執行完畢
for t in thread:
t.join(
)print
("exiting main thread"
)
結果:
startingthread-0
thread-0 獲得鎖
startingthread-1
startingthread-2
thread-0:tue mar 3 22:35:09 2020
thread-0:tue mar 3 22:35:10 2020
thread-0:tue mar 3 22:35:11 2020
thread-0 釋放鎖
exitingthread-0
thread-1 獲得鎖
thread-1:tue mar 3 22:35:13 2020
thread-1:tue mar 3 22:35:15 2020
thread-1:tue mar 3 22:35:17 2020
thread-1 釋放鎖
exitingthread-1
thread-2 獲得鎖
thread-2:tue mar 3 22:35:20 2020
thread-2:tue mar 3 22:35:23 2020
thread-2:tue mar 3 22:35:26 2020
thread-2 釋放鎖
exitingthread-2
exiting main thread
process finished with exit code -1
python 多執行緒程式設計
一 執行緒基礎 1 建立執行緒 thread模組提供了start new thread函式,用以建立執行緒。start new thread函式成功建立後還可以對其進行操作。其函式原型 start new thread function,atgs kwargs 其引數含義如下 args 元組形式的引...
python 多執行緒程式設計
一 執行緒基礎 1 建立執行緒 thread模組提供了start new thread函式,用以建立執行緒。start new thread函式成功建立後還能夠對其進行操作。其函式原型 start new thread function,atgs kwargs 其引數含義例如以下 args 元組形式...
Python多執行緒程式設計
import threading import time deffunc name time.sleep 3 print 子執行緒 s 啟動 threading.current thread name print hello name print 子執行緒 s 結束 threading.curren...