[英]PHP fork, and get result from child
我需要一個例程同時發出2個或更多dns請求,但只使用第一個/最快結果。
我如何退回孩子的回答,所以我只會得到一個結果?
$pid = pcntl_fork();
if ($pid == -1) die("could not fork");
if ($pid) {
//parent
} else {
$pid2 = pcntl_fork();
if ($pid2 == -1) die("could not fork");
if ($pid2) {
//parent-child
exec("/usr/bin/dig @8.8.8.8 $name A +short +time=1+tries=1",$ips_array,$digreturn);
} else {
exec("/usr/bin/dig @8.8.4.4 $name A +short +time=1 +tries=1",$ips_array,$digreturn);
}
}
print_r ($ips_array);
如果兩個請求都被返回,它將返回答案的兩倍。
我建議看一下socket_create_pair() 。
在PHP手冊中,是fork()-parent和child之間的進程間通信(IPC)的非常簡短的示例。
Abd使用serialize()和unserialize()甚至可以傳輸復雜的數據類型,例如數組...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.