这串调用里几个会卡循环

👁️ 2 人浏览 💬 0 人评论 ❤️ 添加收藏

(每道题开头都有同一段:BLOCKING 是会卡住事件循环的同步调用集合(time.sleep、requests.get…);is_blocking(call) 判一个调用卡不卡;blocking_count 数几个卡;first_blocking 第一个卡的。)

一串调用 time.sleep、await_fetch、requests.get、compute,数几个会卡:

BLOCKING = {"time.sleep", "requests.get", "socket.recv", "input"}


def is_blocking(call):
    """这个调用会不会卡住事件循环:同步的 time.sleep / requests.get 等会卡;await 版不卡。"""
    return call in BLOCKING


def blocking_count(calls):
    """一串调用里有几个会卡住事件循环。"""
    return sum(1 for c in calls if is_blocking(c))


def first_blocking(calls):
    """第一个会卡住循环的调用;没有交回空串。"""
    for c in calls:
        if is_blocking(c):
            return c
    return ""

print(blocking_count(["time.sleep", "await_fetch", "requests.get", "compute"]))
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论