我在这里看一个非常有趣的网站 https://faust.readthedocs.io/en/latest/,我对这段代码感到惊讶:
class Order(faust.Record):
account_id: str
product_id: str
price: float
quantity: float = 1.0
orders_topic = app.topic('orders', key_type=str, value_type=Order)
@app.agent(orders_topic)
async def process_order(orders):
async for order in orders:
# process each order using regular Python
total_price = order.price * order.quantity
await send_order_received_email(order.account_id, order)
我的问题是async for order in orders
怎么工作?
哪个版本的Python引入了这种语法?
答案 0 :(得分:0)
它是在Python 3.5中实现的:
https://www.python.org/dev/peps/pep-0492/#asynchronous-iterators-and-async-for
Python版本:3.5
异步iterable可以在其iter实现中调用异步代码,而异步iterator可以在其next方法中调用异步代码。