協程demo,1異步爬網頁 2異步socket請求
阿新 • • 發佈:2018-09-26
int spa __name__ monkey == .org www nal ddr
一、異步爬網頁
‘‘‘ 協程並發爬網頁 ‘‘‘ from urllib import request import gevent,time from gevent import monkey # 讓gevent知道urllib裏的哪些操作屬於IO操作 monkey.patch_all() # 標記當前程序所有的IO操作 def f(url): print("GET:{0}".format(url)) resp = request.urlopen(url) data = resp.read() print(‘{0} bytes received from {1}‘.format(len(data), url)) time_start = time.time() f("https://www.python.org") f("https://www.yahoo.com") f("https://github.com") print("同步耗時:{0}".format(time.time()-time_start)) async_time_start = time.time() gevent.joinall([gevent.spawn(f,"https://www.python.org"), gevent.spawn(f, "https://www.yahoo.com"), gevent.spawn(f, "https://github.com")]) print("異步耗時:{0}".format(time.time()-async_time_start)) ‘‘‘ GET:https://www.python.org 49060 bytes received from https://www.python.org GET:https://www.yahoo.com 498196 bytes received from https://www.yahoo.com GET:https://github.com 64978 bytes received from https://github.com 同步耗時:10.61960744857788 GET:https://www.python.org GET:https://www.yahoo.com GET:https://github.com 86167 bytes received from https://github.com 49060 bytes received from https://www.python.org 503102 bytes received from https://www.yahoo.com 異步耗時:3.7582149505615234‘‘‘
二、異步處理socket請求
服務端:
import socket import gevent from gevent import monkey monkey.patch_all() def server(port): s = socket.socket() s.bind((‘0.0.0.0‘, port)) s.listen(500) while True: cli, addr = s.accept() # 之前多並發是啟動一個線程 gevent.spawn(handle_request, cli) def handle_request(conn): try: while True: data = conn.recv(1024) print("recv:", data) conn.send(data) if not data: conn.shutdown(socket.SHUT_WR) except Exception as ex: print(ex) finally: conn.close() if __name__ == ‘__main__‘: server(8001)
客戶端:
import socket HOST = ‘localhost‘ PORT = 8001 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) while True: msg = bytes(input(">>").strip(), encoding="utf-8") s.sendall(msg) data = s.recv(1024) print("Received:", repr(data)) s.close()
協程demo,1異步爬網頁 2異步socket請求