![等待 HTTP 响应时的 Linux 进程状态](https://linux22.com/image/719592/%E7%AD%89%E5%BE%85%20HTTP%20%E5%93%8D%E5%BA%94%E6%97%B6%E7%9A%84%20Linux%20%E8%BF%9B%E7%A8%8B%E7%8A%B6%E6%80%81.png)
在 Linux 中,当进程(curl、postman、mule、caml)发送 HTTP 请求并等待 HTTP 响应时,其状态会发生什么变化?如何知道进程等待响应的时间?
答案1
在问题更新后,我的评论是 -
取决于具体是什么生成了该请求。例如,如果您在 Angular 中开发或在 Linux 上托管 Angular 应用,则任何 http 请求都是异步的,并且可能需要几秒钟才能显示数据。Angular 通过做出承诺然后在返回数据时履行承诺来处理此问题。处理起来非常麻烦。在 Java 中,一些 http 请求库是同步的,一些是异步的。PHP、cURL 等都是同步的。所以...这取决于什么?
仍然适用。标头已发送,但正在等待数据传输开始?等待标头?一半数据已发送,但传输暂时暂停?
无论如何,curl
报告为“睡觉”,不想为其他人编写更多代码并进行实验。也许某种将自愿上下文切换与经过时间联系起来的方法会起作用?
使用curl
和一个简单的脚本 -
<?php
sleep(60);
print("done");
?>
acurl http://localhost/wait.php
显示/proc/PID/status
它处于休眠状态。我看不到时间指示器 - 这是输出,请注意,我必须启动 curl、单击以更改焦点等,因此需要花费几秒钟的时间
user@darkstar /proc/4816 $ cat status
Name: curl
Umask: 0022
State: S (sleeping)
Tgid: 4816
Ngid: 0
Pid: 4816
PPid: 4729
TracerPid: 0
Uid: 1000 1000 1000 1000
Gid: 1000 1000 1000 1000
FDSize: 256
Groups: 4 24 27 30 44 46 113 130 132 135 1000
NStgid: 4816
NSpid: 4816
NSpgid: 4816
NSsid: 4729
VmPeak: 225592 kB
VmSize: 162176 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 5692 kB
VmRSS: 5616 kB
RssAnon: 800 kB
RssFile: 4816 kB
RssShmem: 0 kB
VmData: 9172 kB
VmStk: 136 kB
VmExe: 176 kB
VmLib: 10392 kB
VmPTE: 184 kB
VmPMD: 12 kB
VmSwap: 0 kB
HugetlbPages: 0 kB
Threads: 1
SigQ: 0/31484
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000001000
SigCgt: 0000000180000000
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000003fffffffff
CapAmb: 0000000000000000
Seccomp: 0
Cpus_allowed: ffff
Cpus_allowed_list: 0-15
Mems_allowed: 00000000,00000001
Mems_allowed_list: 0
voluntary_ctxt_switches: 33
nonvoluntary_ctxt_switches: 2
并且(更好的)time
命令运行 -
user@darkstar:~ $ /usr/bin/time -v curl http://localhost/wait.php
Done Command being timed: "curl http://localhost/wait.php"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 0%
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:00.01
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 7596
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 403
Voluntary context switches: 63
Involuntary context switches: 2
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0