Well. 出色地。
I am so excited to welcome you to week two of our time together in this journey into Agents Land. 我非常高兴地欢迎您来到我们一起踏上特工之地之旅的第二周。
And as well, you know, week two is all about open AI agents SDK, formerly known as swarm, released 另外，您知道，第二周的主题是开放 AI 代理 SDK（以前称为 swarm），已发布
very recently, the newest of our arrivals into the agents Framework land and one that I cannot wait 最近，我们最新的成员进入了代理框架领域，我已经等不及了
to tell you all about. 告诉你一切。
But first, isn't there always a but first but first, before we can do open AI agents SDK, I have 但是首先，不是总有一个但是首先但是首先，在我们可以做开放的AI代理SDK之前，我有
a sidebar, a topic that I have to cover with you, a very important topic, and it's to talk to you 一个侧边栏，一个我要跟你讲的话题，一个非常重要的话题，就是要和你谈谈
about asynchronous Python async IO. 关于异步 Python 异步 IO。
It's something which is common across all of the agent frameworks. 这是所有代理框架中共有的东西。
So all of them make use of asynchronous Python, and it's something which you can get by without really 所以它们都使用异步Python，而且你不需要真正的就可以做到这一点
understanding. 理解。
There's a couple of rules that you can you can learn and then you just follow those rules always. 您可以学习一些规则，然后始终遵循这些规则。
And it will just kind of work and that's okay. 这会起作用，没关系。
But I'm here to tell you that that's unsatisfactory and that it's much better to just take half an hour 但我在这里告诉你，这并不令人满意，最好只花半个小时
to get get to the bottom of this, to really understand it, to thrash it out until you're like, okay, 深入了解这个问题，真正理解它，仔细研究它，直到你觉得，好吧，
I get asynchronous Python, I understand it, and if you do this, if you take the half an hour, you 我得到了异步Python，我理解它，如果你这样做，如果你花半个小时，你
will thank me. 会感谢我的。
It will again and again you will come across this and you'll you will be completely comfortable with 你会一次又一次地遇到这个，你会完全适应
it. 它。
And it's only half an hour. 而且只有半个小时。
And so I've put a guide and the guides that will take you through this, so that you can be left with 因此，我提供了一份指南，以及将引导您完成此过程的指南，以便您可以
no questions in your mind at all about what it means to write asynchronous Python. 您对编写异步 Python 意味着什么毫无疑问。
And I'm also now going to cover it just very briefly at a high level. 我现在也将在高层次上非常简短地介绍它。
Let's talk async IO. 我们来谈谈异步IO。
Well, look, first of all there is like a short version, a simple version. 嗯，看，首先有一个简短的版本，一个简单的版本。
This is what I mean when I say you can get by without really understanding it. 这就是我说即使不真正理解也能过得去的意思。
So async IO is a way of writing Python code, which is a kind of lightweight version of multithreading. 所以async IO是一种编写Python代码的方式，它是多线程的一种轻量级版本。
So many people from a software engineering background will be familiar with the idea of multithreading. 许多具有软件工程背景的人都会熟悉多线程的概念。
When you write code that can run concurrently, you can have multiple threads which are each executing 当您编写可以并发运行的代码时，可以有多个线程，每个线程都在执行
code together, and there's often a lot of baggage that comes with that, some sort of framework stuff 一起编码，通常会带来很多包袱，比如某种框架的东西
that comes with it. 随之而来的。
Well, async IO, which was introduced I think, in Python 3.5, is this very lightweight way of doing 嗯，异步 IO，我认为是在 Python 3.5 中引入的，是一种非常轻量级的做法
it, which doesn't actually involve threads at an operating system level. 它实际上并不涉及操作系统级别的线程。
And it also doesn't involve what's called multiprocessing, which is when you spawn multiple Python 而且它也不涉及所谓的多处理，即生成多个 Python
processes and they all run together. 进程并且它们都一起运行。
This is another way of doing it that is super lightweight. 这是另一种超轻量级的实现方法。
And because it's super lightweight, it means that you can have thousands or tens of thousands of these 而且因为它超轻，这意味着您可以拥有数千或数万个这样的
things all running without consuming much resources at all. 一切都在不消耗太多资源的情况下运行。
So it's a simple, easy peasy, lightweight way of writing code that can run concurrently. 因此，这是一种简单、轻松、轻量级的编写可以并发运行的代码的方法。
And particularly it's good when you have code that makes use of of input output, like waiting on networks. 尤其是当您的代码使用输入输出（例如等待网络）时，它尤其有用。
It allows other things to be running. 它允许其他事物运行。
Whilst one bit of code is waiting on network, and when you're running LM requests, you're mostly. 当一小部分代码在网络上等待时，当您运行 LM 请求时，您大部分时间都在等待。
If you're using paid APIs like OpenAI, most of the time is being spent waiting for stuff to feed back 如果您使用 OpenAI 等付费 API，则大部分时间都花在等待反馈上
from a model running on the cloud. 来自在云上运行的模型。
So there's a lot of waiting on networks, a lot of IO bound waiting. 所以网络上有很多等待，很多 IO 绑定等待。
And as a result, asynchronous code is great to use. 因此，异步代码非常好用。
And when you're thinking of multi-agent frameworks, when potentially you have lots of these all hitting 当您考虑多代理框架时，您可能会遇到很多这样的情况
different APIs, it makes so much sense to be using it. 不同的 API，使用它非常有意义。
And that is why all of the frameworks we'll look at use async IO. 这就是我们将要讨论的所有框架都使用异步 IO 的原因。
Okay. 好的。
So with that preamble this is the short version. 这是序言的简短版本。
The short version is async. 简短的版本是异步的。
IO is actually two things. IO实际上是两个东西。
It's a a package called async IO that you can import, and it's also some language constructs baked 这是一个可以导入的名为 async IO 的包，它也是一些烘焙的语言结构
into Python. 进入Python。
And those language constructs include two keywords that you see right here. 这些语言结构包括您在此处看到的两个关键字。
And one of those keywords is the word async. 这些关键字之一是“异步”一词。
And anytime that you have a function which is potentially going to be able to run in a way that allows 任何时候你有一个函数，它可能能够以允许的方式运行
other things to happen concurrently, you simply put the word async before it like this async def do 其他同时发生的事情，你只需将 async 这个词放在它前面，就像 async def 那样
some processing, so def do some processing becomes async def do some processing. 一些处理，所以 def do someprocessing 变成 async def do someprocessing。
That's how you say this is a function which can run asynchronously. 这就是你所说的这是一个可以异步运行的函数。
And when you call that function you don't just call do some processing like you're used to. 当您调用该函数时，您不只是像以前那样调用进行一些处理。
You have to put the word await before it. 您必须在其前面加上“await”一词。
So you say await, do some processing and if do some processing returns a string, then you can't just 所以你说等待，做一些处理，如果做一些处理返回一个字符串，那么你不能只是
say result equals do some processing. 说结果等于做一些处理。
You have to say result equals await. 你必须说结果等于等待。
Do some processing. 做一些处理。
But otherwise it's just the same. 但除此之外都是一样的。
So that's the short version. 这就是简短的版本。
And if you wanted to get by without really understanding it, you could just follow those rules and 如果你想在没有真正理解它的情况下度过难关，你可以遵循这些规则并
keep going and it might be enough. 继续下去，可能就足够了。
But now let me tell you the bigger story. 但现在让我告诉你更大的故事。
Okay. 好的。
Now we're going to go a little bit deeper and talk about the real story with async IO. 现在我们将更深入地讨论异步 IO 的真实故事。
So look as I say, it's a lightweight alternative to multithreading or multiprocessing. 所以正如我所说，它是多线程或多处理的轻量级替代方案。
Something like multithreading is implemented at the OS level. 诸如多线程之类的东西是在操作系统级别实现的。
And it supports concurrent execution of different programs where the CPU, the CPU level that's being 并且它支持不同CPU、CPU级别的程序的并发执行
managed to sort of switch between these two programs and treat them as if they're running at the same 设法在这两个程序之间进行切换，并将它们视为同时运行
time. 时间。
This is different in the case of async IO. 这在异步 IO 的情况下有所不同。
So there are some some special keywords. 所以有一些特殊的关键词。
There's this keyword async. 有这个关键字async。
If you define a function as async def and then the function instead of just def the function, then 如果您将函数定义为 async def，然后定义该函数，而不是仅定义该函数，那么
this thing is no longer actually called a function. 这个东西实际上已经不叫函数了。
It's known as a coroutine. 它被称为协程。
Now most people still use the word function. 现在大多数人仍然使用函数这个词。
You'll hear me saying function, but strictly speaking, anytime you see async def, you should say, 你会听到我说函数，但严格来说，每当你看到 async def 时，你应该说，
okay, that's not a function, it is a coroutine. 好吧，这不是一个函数，它是一个协程。
And that means it's something special that that Python can pause and resume. 这意味着 Python 可以暂停和恢复，这是很特别的。
When you call a coroutine, it doesn't run unlike any other function that you call that does run. 当您调用协程时，它不会像您调用的任何其他运行的函数那样运行。
When you call a coroutine, it just returns a coroutine object, but nothing is executed to actually 当你调用协程时，它只是返回一个协程对象，但实际上并没有执行任何操作
run that coroutine object. 运行该协程对象。
There are a few ways to do it, but the most common one, and the one that we will use all the time 有几种方法可以做到这一点，但最常见的一种，也是我们将一直使用的一种
is to await it. 就是等待它。
So you say await, and then the coroutine object and that schedules it for execution. 所以你说await，然后是协程对象并安排它执行。
What does it mean to say it's scheduled for execution. 计划执行是什么意思？
Well, there is this piece of code written in the async IO library, which does what's known as an event 嗯，异步 IO 库中有一段代码，它执行所谓的事件
loop, which means it's a kind of while loop that iterates and it takes this coroutine and it starts 循环，这意味着它是一种迭代的 while 循环，它采用这个协程并开始
executing it, and it can only execute one coroutine at a time. 执行它，并且它一次只能执行一个协程。
It's not like multithreading. 它不像多线程。
It's going to be executing this coroutine, except if this coroutine gets to a point when it's stopped 它将执行该协程，除非该协程到达停止点
and it's waiting for something like it's waiting for IO. 它正在等待类似等待 IO 的东西。
Perhaps. 也许。
Perhaps it's made a call to OpenAI and it's waiting for OpenAI to respond. 也许它已向 OpenAI 发出了呼叫，并且正在等待 OpenAI 响应。
At that point, the event loop can put that on pause and start executing a different one of the coroutines 此时，事件循环可以暂停该事件并开始执行另一个协程
that's in its sort of list of coroutines waiting to be run. 它位于等待运行的协程列表中。
And if that hits a point when it's waiting on on IO, then the event loop can run another one or it 如果在等待 IO 时达到某一点，那么事件循环可以运行另一个事件循环或者它
can continue the first one. 可以继续第一个。
So it's a sort of manual way of handling multithreading. 所以这是一种处理多线程的手动方法。
It's a manual approach to implementing multithreading, but only really working when something is blocked. 这是一种实现多线程的手动方法，但只有在某些内容被阻塞时才真正起作用。
Waiting on I o. 等待我o。
And because of this, because it's sort of written at a code level and it's super simple and very easy 正因为如此，因为它是在代码级别编写的，并且非常简单且非常容易
to understand, it's also lightweight. 要理解，它也是轻量级的。
You can have thousands or tens of thousands of these things. 你可以拥有数千或数万个这样的东西。
And, and so it's and it's really easy and quick to get this running. 而且，运行起来非常简单快捷。
And that is why it's so popular. 这就是它如此受欢迎的原因。
And that is why it's used so ubiquitously across a genetic frameworks. 这就是为什么它在遗传框架中如此普遍地使用。
So I hope obviously I've only given you a bit of detail here, but I hope that gives you some perspective 所以我希望显然我在这里只给了你一些细节，但我希望这能给你一些观点
on what's going on. 关于正在发生的事情。
And when we look back now at at this, you can see that that whilst a simple interpretation is just 当我们现在回顾这一点时，你会发现虽然简单的解释只是
to say, okay, whenever I use async I've got to use await. 好吧，每当我使用 async 时，我就必须使用 wait。
The deeper interpretation is to understand that when I say async def do some processing that's no longer 更深入的解释是要理解当我说 async def 做一些不再是的处理时
a function, it's now a coroutine do some processing. 一个函数，它现在是一个协程，做一些处理。
We need to call await, do some processing in order to schedule that coroutine. 我们需要调用await，进行一些处理以安排该协程。
And that is now blocking until that completes and the result will go into that variable result. 现在正在阻塞，直到完成并且结果将进入该变量结果。
I hope that made some sense, but if not, as I say, there's a whole guide. 我希望这有一定道理，但如果没有，正如我所说，这里有一个完整的指南。
You should go and look at the guide now. 你现在应该去看看指南。
And so just to beat this one to death, I'll give you a couple more examples. 为了彻底解决这个问题，我再举几个例子。
And then hopefully you've really got this. 然后希望你真的明白了。
So look at this again. 所以再看看这个。
This is back to the async def do some processing. 这又回到async def做一些处理了。
It's going to do some work. 它将做一些工作。
And then it's going to return the string done. 然后它将返回完成的字符串。
If I just say like my coroutine equals do some processing, you might, you know, if you didn't know 如果我只是说我的协程等于做一些处理，你可能，你知道，如果你不知道
already about this, you might think that that will run do some processing. 已经了解了这一点，您可能会认为这将运行一些处理。
But of course it will not. 但当然不会。
It will return a coroutine object and that's what will go into the variable, my coroutine. 它将返回一个协程对象，这就是变量（我的协程）中的内容。
In order to actually run it you have to call await. 为了实际运行它，你必须调用await。
So you'd say my result equals await my coroutine. 所以你会说我的结果等于等待我的协程。
It's now going to run. 现在它要运行了。
And the result? 结果呢？
The string done is going to go into my result. 完成的字符串将进入我的结果。
So that is the point. 这就是重点。
That's how it works. 这就是它的工作原理。
And of course you don't need to do that. 当然你不需要这样做。
It's simpler than that. 比那更简单。
You can just say my result equals await. 你可以说我的结果等于等待。
Do some processing, which is what we always do. 做一些处理，这是我们一直做的事情。
And then just to hopefully sort of connect the dots to make this really click for you. 然后只是希望将这些点连接起来，让这真正适合你。
There are other constructs you don't just have to call await. 还有其他构造，您不必只调用 wait。
And then the name of of a coroutine, because if that's all you could do, you might think, you might 然后是协程的名称，因为如果这就是你所能做的，你可能会想，你可能
say to me, okay, hang on. 对我说，好吧，坚持住。
But in that case there's no real there's nothing concurrent happening here. 但在这种情况下，这里就没有真正发生任何并发事件。
Every time I call await and then a coroutine, it will block until that finishes and then it will go 每次我调用await，然后调用协程时，它都会阻塞，直到完成，然后它就会继续
on to the next. 继续下一步。
Well, there are a few other constructs in the async IO package that allow us to be more to do more 嗯，异步 IO 包中还有一些其他构造，可以让我们做得更多
interesting things, and this is one of them, and it's one that we will use this week in our code. 有趣的事情，这是其中之一，也是我们本周将在代码中使用的事情。
And it's where you can say results, plural Results equals await. 在这里你可以说结果，复数结果等于等待。
And then call this this function Asyncio dot gather. 然后调用这个函数Asyncio dot Gather。
And you can then pass in multiple coroutines. 然后你可以传入多个协程。
And I'm sure you can imagine what happens. 我相信你可以想象会发生什么。
But the event loop is going to schedule all three. 但事件循环将安排这三个事件。
And as soon as one of them is blocking, the others will start running, or one of the others will run 一旦其中一个阻塞，其他的就会开始运行，或者其他的一个会运行
until it's blocking, and then the third one will run. 直到它被阻塞，然后第三个将运行。
And so all three of those coroutines will, will, will run, will execute. 因此，所有这三个协程将、将、将运行、将执行。
And the results as a list will go into the variable results, a list of each of the three results from 结果作为列表将进入变量 results，即来自的三个结果中的每一个的列表
each of those three coroutines. 这三个协程中的每一个。
So in some ways it's kind of fake multithreading. 所以在某些方面它是一种假多线程。
It's sort of brute force multithreading. 这是一种强力多线程。
It's multithreading not at the operating system level, but multithreading implemented almost manually 它不是操作系统级别的多线程，而是几乎手动实现的多线程
with this event loop, and just handling things like IO blocking and being able to schedule the next 使用此事件循环，只需处理 IO 阻塞之类的事情并能够安排下一个
one. 一。
That's how to think about it. 就是这样想的。