第一个卡住的是哪个
(每道题开头都有同一段:BLOCKING 是会卡住事件循环的同步调用集合(time.sleep、requests.get…);is_blocking(call) 判一个调用卡不卡;blocking_count 数几个卡;first_blocking 第一个卡的。)
调用序列 fetch_x、requests.get、time.sleep,问第一个会卡的:
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(first_blocking(["fetch_x", "requests.get", "time.sleep"]))
全部评论