<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # async/await 用`asyncio`提供的`@asyncio.coroutine`可以把一個generator標記為coroutine類型,然后在coroutine內部用`yield from`調用另一個coroutine實現異步操作。 為了簡化并更好地標識異步IO,從Python 3.5開始引入了新的語法`async`和`await`,可以讓coroutine的代碼更簡潔易讀。 請注意,`async`和`await`是針對coroutine的新語法,要使用新的語法,只需要做兩步簡單的替換: 1. 把`@asyncio.coroutine`替換為`async`; 2. 把`yield from`替換為`await`。 讓我們對比一下上一節的代碼: ``` @asyncio.coroutine def hello(): print("Hello world!") r = yield from asyncio.sleep(1) print("Hello again!") ``` 用新語法重新編寫如下: ``` async def hello(): print("Hello world!") r = await asyncio.sleep(1) print("Hello again!") ``` 剩下的代碼保持不變。 ## 小結 Python從3.5版本開始為`asyncio`提供了`async`和`await`的新語法; 注意新語法只能用在Python 3.5以及后續版本,如果使用3.4版本,則仍需使用上一節的方案。 ## 練習 將上一節的異步獲取sina、sohu和163的網站首頁源碼用新語法重寫并運行。 ## 參考源碼 [async_hello2.py](https://github.com/michaelliao/learn-python3/blob/master/samples/async/async_hello2.py) [async_wget2.py](https://github.com/michaelliao/learn-python3/blob/master/samples/async/async_wget2.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>

                              哎呀哎呀视频在线观看