<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                `asyncio`是Python 3.4版本引入的標準庫,直接內置了對異步IO的支持。 `asyncio`的編程模型就是一個消息循環。我們從`asyncio`模塊中直接獲取一個`EventLoop`的引用,然后把需要執行的協程扔到`EventLoop`中執行,就實現了異步IO。 用`asyncio`實現`Hello world`代碼如下: ~~~ import asyncio @asyncio.coroutine def hello(): print("Hello world!") # 異步調用asyncio.sleep(1): r = yield from asyncio.sleep(1) print("Hello again!") # 獲取EventLoop: loop = asyncio.get_event_loop() # 執行coroutine loop.run_until_complete(hello()) loop.close() ~~~ `@asyncio.coroutine`把一個generator標記為coroutine類型,然后,我們就把這個`coroutine`扔到`EventLoop`中執行。 `hello()`會首先打印出`Hello world!`,然后,`yield from`語法可以讓我們方便地調用另一個`generator`。由于`asyncio.sleep()`也是一個`coroutine`,所以線程不會等待`asyncio.sleep()`,而是直接中斷并執行下一個消息循環。當`asyncio.sleep()`返回時,線程就可以從`yield from`拿到返回值(此處是`None`),然后接著執行下一行語句。 把`asyncio.sleep(1)`看成是一個耗時1秒的IO操作,在此期間,主線程并未等待,而是去執行`EventLoop`中其他可以執行的`coroutine`了,因此可以實現并發執行。 我們用Task封裝兩個`coroutine`試試: ~~~ import threading import asyncio @asyncio.coroutine def hello(): print('Hello world! (%s)' % threading.currentThread()) yield from asyncio.sleep(1) print('Hello again! (%s)' % threading.currentThread()) loop = asyncio.get_event_loop() tasks = [hello(), hello()] loop.run_until_complete(asyncio.wait(tasks)) loop.close() ~~~ 觀察執行過程: ~~~ Hello world! (<_MainThread(MainThread, started 140735195337472)>) Hello world! (<_MainThread(MainThread, started 140735195337472)>) (暫停約1秒) Hello again! (<_MainThread(MainThread, started 140735195337472)>) Hello again! (<_MainThread(MainThread, started 140735195337472)>) ~~~ 由打印的當前線程名稱可以看出,兩個`coroutine`是由同一個線程并發執行的。 如果把`asyncio.sleep()`換成真正的IO操作,則多個`coroutine`就可以由一個線程并發執行。 我們用`asyncio`的異步網絡連接來獲取sina、sohu和163的網站首頁: ~~~ import asyncio @asyncio.coroutine def wget(host): print('wget %s...' % host) connect = asyncio.open_connection(host, 80) reader, writer = yield from connect header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host writer.write(header.encode('utf-8')) yield from writer.drain() while True: line = yield from reader.readline() if line == b'\r\n': break print('%s header > %s' % (host, line.decode('utf-8').rstrip())) # Ignore the body, close the socket writer.close() loop = asyncio.get_event_loop() tasks = [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']] loop.run_until_complete(asyncio.wait(tasks)) loop.close() ~~~ 執行結果如下: ~~~ wget www.sohu.com... wget www.sina.com.cn... wget www.163.com... (等待一段時間) (打印出sohu的header) www.sohu.com header > HTTP/1.1 200 OK www.sohu.com header > Content-Type: text/html ... (打印出sina的header) www.sina.com.cn header > HTTP/1.1 200 OK www.sina.com.cn header > Date: Wed, 20 May 2015 04:56:33 GMT ... (打印出163的header) www.163.com header > HTTP/1.0 302 Moved Temporarily www.163.com header > Server: Cdn Cache Server V2.0 ... ~~~ 可見3個連接由一個線程通過`coroutine`并發完成。 ### 小結 `asyncio`提供了完善的異步IO支持; 異步操作需要在`coroutine`中通過`yield from`完成; 多個`coroutine`可以封裝成一組Task然后并發執行。 ### 參考源碼 [async_hello.py](https://github.com/michaelliao/learn-python3/blob/master/samples/async/async_hello.py) [async_wget.py](https://github.com/michaelliao/learn-python3/blob/master/samples/async/async_wget.py)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看