您的位置:首页 > 编程语言 > Python开发

Python Tulip ( asyncio) 第1节 Base Event Loop 基本事件循环(1) 译文

2015-01-28 11:23 471 查看

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(delay, callback, *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(when, callback, *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()函数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: