本篇内容
- udp协议套接字
- 开启进程的方式
- 多进程实现并发的套接字通信
- join方法
- 守护进程
- 同步锁
- 进程队列
- 生产者消费者模型
- 进程池
- paramiko模块
一、 udp协议套接字
1.TCP和UDP在传输层区别:
UDP是无连接不可靠的数据报协议。TCP提供面向连接的可靠字节流。
2.使用UDP常见应用:
DNS(域名系统),NFS(网络文件系统),SNMP(简单网络管理协议)。
3.代码应用:
服务端:
#!/usr/binl/env python #encoding: utf-8 #author: YangLei import socketserver class MyUDPhandler(socketserver.BaseRequestHandler): def handle(self): print(self.request) self.request[1].sendto(self.request[0].upper(),self.client_address) if __name__ == '__main__': s = socketserver.ThreadingUDPServer(('127.0.0.1',8080),MyUDPhandler) s.serve_forever()
客户端:
#!/usr/binl/env python #encoding: utf-8 #author: YangLei from socket import * udp_client = socket(AF_INET,SOCK_DGRAM) while True: msg=input('>>: ').strip() udp_client.sendto(msg.encode('utf-8'),('127.0.0.1',8080)) data,server_addr = udp_client.recvfrom(1024) print(data.decode('utf-8'))
注意:但这种方式并不能控制客户端的并发数量,并发数量达到一定数量后,服务端会down掉,解决办法后续会提供。
二、开启进程的方式
开启进程的方式分为两种:
(1)利用模块开启进程:
#!/usr/binl/env python #encoding: utf-8 #author: YangLei from multiprocessing import Process import time def work(name): print('task <%s> is runing' %name) time.sleep(2) print('task <%s> is done' % name) if __name__ == '__main__': p1 = Process(target=work,args=('xiaolan',)) p2 = Process(target=work,args=('xiaohong',)) p1.start() p2.start() print('主程序')
(2)利用类开启进程:
#!/usr/binl/env python #encoding: utf-8 #author: YangLei from multiprocessing import Process import time class MyProcess(Process): def __init__(self,name): super().__init__() self.name = name def run(self): print('task <%s> is runing' % self.name) time.sleep(2) print('task <%s> is done' % self.name) if __name__ == '__main__': p = MyProcess('xiaolan') p.start() print('主程序')
三、多进程实现并发的套接字通信
基于刚刚学习的开启进程的方式,咱们就用进程的方式来开启一个网络通信。
服务端:
#!/usr/binl/env python #encoding: utf-8 #author: YangLei from multiprocessing import Process from socket import * s = socket(AF_INET,SOCK_STREAM) s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) s.bind(('127.0.0.1',8080)) s.listen(5) def talK(conn,addr): while True: try: data=conn.recv(1024) if not data:break conn.send(data.upper()) except Exception: break conn.close() if __name__ == '__main__': while True: conn,addr = s.accept() p=Process(target=talK,args=(conn,addr)) p.start() s.close()
客户端:
#!/usr/binl/env python #encoding: utf-8 #author: YangLei from socket import * c = socket(AF_INET,SOCK_STREAM) c.connect(('127.0.0.1',8080)) while True: msg = input('>>: ').strip() if not msg:continue c.send(msg.encode('utf-8')) data = c.recv(1024) print(data.decode('utf-8')) c.close()
四、join方法
1.定义:
(1)join方法的作用是阻塞主进程(挡住,无法执行join以后的语句),专注执行多进程。
(2)多进程多join的情况下,依次执行各进程的join方法,前头一个结束了才能执行后面一个。
(3)无参数,则等待到该进程结束,才开始执行下一个进程的join。
2.代码:
#!/usr/binl/env python #encoding: utf-8 #author: YangLei from multiprocessing import Process import time def work(name): print('task <%s> is runing' %name) time.sleep(3) print('task <%s> is done' % name) if __name__ == '__main__': p1 = Process(target=work,args=('xiaolan',)) p2 = Process(target=work,args=('xiaohong',)) p3 = Process(target=work,args=('xiaolv',)) p_list = [p1, p2, p3] for p in p_list: p.start() for p in p_list: p.join() print('主进程')
五、守护进程
1.定义:
(1)守护进程是主程序创建的。
(2)守护进程会在主进程代码执行结束后就终止。
(3)守护进程内无法再开启子进程,否则抛出异常:
AssertionError: daemonic processes are not allowed to have children。
2.代码:
#!/usr/binl/env python #encoding: utf-8 #author: YangLei from multiprocessing import Process import time def work(name): print('task <%s> is runing' %name) time.sleep(2) print('task <%s> is done' % name) if __name__ == '__main__': p1 = Process(target=work,args=('xiaolan',)) p1.daemon = True p1.start() print('主程序')
六、同步锁
1.定义:
通常被用来实现共享资源的同步访问,为每一个共享资源创建一个Lock对象当你需要访问该资源时,调用qcuqire方法来获取锁对象(如果其他线程已经获得该锁,则当前线程需等待期被释放),待资源访问完后,在调用release方法释放锁。
2.代码:
#!/usr/binl/env python #encoding: utf-8 #author: YangLei from multiprocessing import Process,Lock import time def work(name,mutex): mutex.acquire() print('task <%s> is runing' %name) time.sleep(2) print('task <%s> is done' % name) mutex.release() if __name__ == '__main__': mutex = Lock() p1 = Process(target=work,args=('xiaolan',mutex)) p2 = Process(target=work,args=('xiaohong',mutex)) p1.start() p2.start() print('主程序')
3.代码应用:
模拟抢票过程
python代码:
#!/usr/binl/env python #encoding: utf-8 #author: YangLei import json import os import time from multiprocessing import Process,Lock def search(): dic = json.load(open('db.txt')) print(' 33[32m[%s] 看到剩余票数<%s>