python asyncio as_completed

# ASYNCIO http protocol does not provide an interface aiohttp 
Import ASYNCIO
 Import socket
 from The urllib.parse Import the urlparse 


the async DEF GET_URL (URL):
     # through the socket request HTML 
    URL = the urlparse (URL) 
    Host = url.netloc 
    path = url.path
     IF path = = "" : 
        path = " / " 

    # establish a socket connection 
    Reader, Writer = the await asyncio.open_connection (Host, 80 ) 
    writer.Write ( "GET {} HTTP/1.1\r\nHost:{}\r\nConnection:close\r\n\r\n".format(path, host).encode("utf8"))
    all_lines = []
    async for raw_line in reader:
        data = raw_line.decode("utf8")
        all_lines.append(data)
    html = "\n".join(all_lines)
    return html

async def main():
    tasks = []
    for url in range(20):
        url = " http://shop.projectsedu.com/goods/{}/ " .format (url) 
        tasks.append (asyncio.ensure_future (GET_URL (url))) 
    for Task in asyncio.as_completed (Tasks): # similar to task threads in the pool as 
        the Result = await task # to be used here await wait for its completion 
        Print (the Result) 

IF  __name__ == " __main__ " :
     Import Time 
    start_time = time.time () 
    Loop = asyncio.get_event_loop () 
    loop.run_until_complete (main ()) 
    Print ('last time:{}'.format(time.time()-start_time))

 

Guess you like

Origin www.cnblogs.com/callyblog/p/11222332.html