知识卡片
串行请求相互排队
接收线程同步执行 handle_connection 时,只有它返回后才能继续处理下一条连接。先处理耗时任务会推迟随后很短的任务。加入一个固定延迟的 /sleep 路径可在本地观察这种排队:先启动慢请求,才提交快请求;在顺序执行的版本中,快请求也必须等慢请求完成。
相关卡片 [[线程池限制并发任务]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 最终项目:构建多线程 Web 服务器 Validating the Request and Selectively Responding · 查看原书依据
Start the server using cargo run. Then open two browser windows: one for http://127.0.0.1:7878 and the other for http://127.0.0.1:7878/sleep. If you enter the / URI a few times, as before, you’ll see it respond quickly. But if you enter /sleep and then load /, you’ll see that / waits until sleep has slept for its full five seconds before loading.
参考来源
- 位置:《The Rust Programming Language(第2版)》第20章 最终项目:构建多线程 Web 服务器(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c20.xhtml`,块 0121)
- 结论依据:原文描述了用两个窗口依次打开慢路径和快路径,观察后者也要等待的过程。
- 原始内容:Start the server using cargo run. Then open two browser windows: one for http://127.0.0.1:7878 and the other for http://127.0.0.1:7878/sleep. If you enter the / URI a few times, as before, you’ll see it respond quickly. But if you enter /sleep and then load /, you’ll see that / waits until sleep has slept for its full five seconds before loading.