Python Tulip ( asyncio) 第1節 Base Event Loop 基本事件循環(1) 譯文

Tulip 的 《基本事件循環》第一篇 (共三篇)

這是很長的一篇 所以分成了幾部分

原文地址 http://python.readthedocs.org/en/latest/library/asyncio-eventloop.html


18.5.1. Base Event Loop  基本事件循環

The event loop is the central execution device provided by asyncio. It provides multiple facilities, amongst which:

  • Registering, executing and cancelling delayed calls (timeouts).

  • Creating client and server transports for various kinds of communication.

  • Launching subprocesses and the associated transports for communication with an external program.

  • Delegating costly function calls to a pool of threads.

  • class asyncio.BaseEventLoop

  • Base class of event loops.

事件循環是asyncio提供的中央處理設備。它提供了多種工具,包括下面這些:

○ 註冊、執行和取消延遲調用(超時)。

○ 創建transport的服務端和客戶端,可用於多種類型的通訊。

○ 登錄紫禁城以及關聯外部程序通訊用的transport。

○ 委派耗時的調用到線程池。

 asyncio.BaseEventLoop

  是事件循環的基類。


18.5.1.1. Run an event loop  運行事件循環

  • BaseEventLoop.run_forever()

    Run until stop() is called.

  一直運行,直到調用stop()終止。  

 

  • BaseEventLoop.run_until_complete(future)

    Run until the Future is done.

    If the argument is a coroutine object, it is wrapped by async().

    Return the Future’s result, or raise its exception.

  一直運行到Future完成。

  如果以coroutine object 爲參數,會用async()包裹它。

  返回Future類型結果,或者是發出異常。


  • BaseEventLoop.is_running()

    Returns running status of event loop.

  返回事件循環的運行狀態


  • BaseEventLoop.stop()

    Stop running the event loop.

    Every callback scheduled before stop() is called will run. Callbacks scheduled after stop() is called will not run. However, those callbacks will run if run_forever() is called again later.

    停止事件循環。

    每個回調在stop()運行之前都會預先處理。在stop()運行後不會再安排回調。然而,這些回調在再執行run_forever()後會再度被調用。


  • BaseEventLoop.is_closed()

    Returns True if the event loop was closed.

    New in version 3.4.2.

  在事件循環已經停止時返回True。


  • BaseEventLoop.close()  

    Close the event loop. The loop must not be running.

    This clears the queues and shuts down the executor, but does not wait for the executor to finish.

    This is idempotent and irreversible. No other methods should be called after this one.

  關閉事件循環,該循環必須是在運行中的。

  這個函數清除隊列並關閉executor,但是不會等待executer運行完成。

  它是一個等冪操作*,並且不可逆轉,不能在調用這個方法後再調用其他方法。

  補充:等冪操作:  Idempotency refers to the ability to operate on a resource with the same command any number of times and maintain the same state.  等冪是指對於一個資源上,無限次執行同一個命令,會保持同樣的狀態的一種能力。



18.5.1.2. Calls 調用


Most asyncio functions don’t accept keywords. If you want to pass keywords to your callback, use functools.partial(). For example ,  loop.call_soon(functools.partial(print, "Hello", flush=True))

 will call print("Hello", flush=True).  

多數asyncio 函數不接收關鍵字(參數)。如果需要傳遞關鍵字到回調中,使用functools.partial()例如

loop.call_soon(functools.partial(print, "Hello", flush=True)) 將調用

print("Hello", flush=True)


Note 注意 

functools.partial() is better than lambda functions, because asyncio can inspect functools.partial() object to display parameters in debug mode, whereas lambda functions have a poor representation.

functools.partial()比lambda函數更好,因爲asyncio在調試模式可以檢測functools.partial()中的對象,而lambda函數不能很好表現。


BaseEventLoop.call_soon(callback*args)

 Arrange for a callback to be called as soon as possible. The callback is called after call_soon() returns, when control returns to the event loop.

 安排一個回調能執行的時候立即執行。當控制返回到事件循環,這個回調在call_soon()即可返回。


 This operates as a FIFO queue, callbacks are called in the order in which they are registered. Each callback will be called exactly once.

 這個操作類似FIFO隊列,回調依次被註冊,每個回調會被執行一次。


 Any positional arguments after the callback will be passed to the callback when it is called.

 任何在回調傳入的位置參數,在被調用時都會被傳遞到回調。


 An instance of asyncio.Handle is returned. 一個asyncio.Handle 實例會被返回。

Use functools.partial to pass keywords to the callback. 【鏈接】使用functools.partial傳遞關鍵字型參數給回調。


BaseEventLoop.call_soon_threadsafe(callback*args)

 Like call_soon(), but thread safe. 類似call_soon() ,支持線程安全。




18.5.1.3. Delayed calls 延遲調用

The event loop has its own internal clock for computing timeouts. Which clock is used depends on the (platform-specific) event loop implementation; ideally it is a monotonic clock. This will generally be a different clock than time.time().


Note 注意 

Timeouts (relative delay or absolute when) should not exceed one day.

超時(相對的delay或絕對的when)都不應該超過一天。


  • BaseEventLoop.call_later(delaycallback*args)

    Arrange for the callback to be called after the given delay seconds (either an int or float).

    An instance of asyncio.Handle is returned.

    安排callback回調在給定的延遲delay 秒(需要整型或浮點型)被調用。


    返回一個asyncio.Handle實例。


    callback will be called exactly once per call to call_later(). If two callbacks are scheduled for exactly the same time, it is undefined which will be called first.

    在每次調用 call_later()後,callback會被調用。如果兩個調用被計劃到完全相同的時間執行,無法明確哪個會被先調用。


    The optional positional args will be passed to the callback when it is called. If you want the callback to be called with some named arguments, use a closure or functools.partial().

    當被調用時,可選的位置參數會傳遞給回調。如果你想要回調被調用時傳入某些命名參數,使用閉包模式的functools.partial()


    Use functools.partial to pass keywords to the callback【鏈接】使用functools.partial傳遞關鍵字型參數給回調。


  • BaseEventLoop.call_at(whencallback*args)

    Arrange for the callback to be called at the given absolute timestamp when (an int or float), using the same time reference asBaseEventLoop.time().

    安排回調在給定的絕對時間戳when (一個整數或浮點值),使用相同的時間格式,類似於BaseEventLoop.time()


    This method’s behavior is the same as call_later().


    這個方法的行爲類似於call_later()


    Use functools.partial to pass keywords to the callback【鏈接】使用functools.partial傳遞關鍵字型參數給回調。


  • BaseEventLoop.time()

    Return the current time, as a float value, according to the event loop’s internal clock.


See also 參見

The asyncio.sleep() function. asyncio.sleep()函數。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章