具有异步功能的装饰器

Decorator with async function

我正在尝试在 python 中实现实时网络摄像头服务,所以我正在寻找 aiortc。查看GitHub page上的例子,我发现了一个奇怪的东西,我无法理解它是如何工作的。

在示例文件夹的 server/server.py 上,有一个带有装饰器的 async 函数。该函数从未被调用,所以我不明白装饰器是如何工作的。


    pc = RTCPeerConnection()
    
    .......

    @pc.on("iceconnectionstatechange")
    async def on_iceconnectionstatechange():
        log_info("ICE connection state is %s", pc.iceConnectionState)
        if pc.iceConnectionState == "failed":
            await pc.close()
            pcs.discard(pc)

在这种情况下,永远不会调用函数 on_iceconnectionstatechange@pc.on 装饰器以何种方式被调用?

RTCPeerConnection继承自AsyncIOEventEmitterlink) from the pyee modulepyee是一个事件系统模块。这个AsyncIOEventEmitterclass是on装饰器方法来自

on_iceconnectionstatechange 函数永远不会被直接调用,但是装饰器将其注册为事件侦听器,因此每当该事件发出时都会调用它,例如 here.

由于装饰器的工作原理,问题中的代码片段大致相当于:

decorator = pc.on("iceconnectionstatechange")

async def on_iceconnectionstatechange():
    log_info("ICE connection state is %s", pc.iceConnectionState)
    if pc.iceConnectionState == "failed":
        await pc.close()
        pcs.discard(pc)

on_iceconnectionstatechange = decorator(on_iceconnectionstatechange)

这里是 decorator 的“定义”片段(来自 here):

def _on(f):
    self._add_event_handler(event, f, f)
    return f

这里,event的值为"iceconnectionstatechange",所以代码相当于:

async def on_iceconnectionstatechange():
    log_info("ICE connection state is %s", pc.iceConnectionState)
    if pc.iceConnectionState == "failed":
        await pc.close()
        pcs.discard(pc)

pc._add_event_handler("iceconnectionstatechange", on_iceconnectionstatechange, on_iceconnectionstatechange)

因为调用了decorator函数,它可以在一些内部字典中注册on_iceconnectionstatechange,以便在发出相关事件时调用它。装饰器在没有直接调用 on_iceconnectionstatechange 的情况下注册事件侦听器,因为它在创建时注册它,而不是等待它被调用。