由于之前看书的时候,对多线程的调用不怎么了解,借此机会,看了一些资料自己整理了一下
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=0
class myThread(threading.Thread):
def __init__(self,threadID,name,counter):
threading.Thread.__init__(self)
self.threadID=threadID
self.name=name
self.counter=counter
def run(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)
def print_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):
thread.append(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