python 编程中的术语“阻塞”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2407589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the term "blocking" mean in programming?
提问by alfredo
Could someone provide a layman definition and use case?
有人可以提供一个外行的定义和用例吗?
回答by T.J. Crowder
"Blocking" means that the caller waits until the callee finishes its processing. For instance, a "blocking read" from a socket waits until there is data to return; a "non-blocking" read does not, it just returns an indication (usually a count) of whether there was something read.
“阻塞”意味着调用者等待被调用者完成其处理。例如,来自套接字的“阻塞读取”一直等到有数据返回;“非阻塞”读取不会,它只是返回一个指示(通常是一个计数)是否有读取的东西。
You hear the term mostly around APIs that access resources that don't necessarily require CPU attention -- I/O, for instance. You also hear it in multi-threading: A call from Thread A to Thread B might be designed to "block" (hold up Thread A) until Thread B achieves the relevant state to process or at least accept the request. (The most obvious example there being "join", which usually means "I, Thread A, want to wait until Thread B has terminated" -- you use that when exiting a multi-threaded program.)
您听到的这个术语主要是关于访问不一定需要 CPU 关注的资源的 API —— 例如,I/O。您还可以在多线程中听到:从线程 A 到线程 B 的调用可能被设计为“阻塞”(阻止线程 A),直到线程 B 达到相关状态来处理或至少接受请求。(最明显的例子是“加入”,这通常意味着“我,线程 A,想要等到线程 B 终止”——你在退出多线程程序时使用它。)
回答by friederbluemle
In simple words: If you call a function that stops the program from continuing to run until the user has performed some action (or some other action that your program is not controlling), this call is called a blocking call.
简而言之:如果您调用一个函数,该函数会阻止程序继续运行,直到用户执行某些操作(或您的程序未控制的其他操作),此调用称为阻塞调用。