php - parallel process with popen -
i need download files many web servers , somethigs. files big. each downloads it's independent want in parallel.
so write following scripts:
test.php
$urls = [ 'url1', 'url2', 'url3' ]; foreach($urls $url){ popen("php download.php --url=" . $url, "w"); } download.php
$options = getopt(null, ["url:"]); $url = $options["url"]; // somethings - loop testing purposes for($i = 0; $i <= 1000000000; $i++){ $i; } print parse_url($url)["host"]; when run php test.php console show me sequencial output. doing wrong?
if want them run in async use this, run each in background.
popen("php download.php --url=" . $url . " &", "w"); demo:
<?php $urls = ['www.baidu.com', 'github.com', 'stackoverflow.com']; foreach($urls $url) { var_dump(popen("ping -c4 " . $url . " &", "w")); echo "dddd\n"; } output:
ei@localhost:~$ php test.php resource(4) of type (stream) dddd resource(5) of type (stream) dddd resource(6) of type (stream) dddd ei@localhost:~$ ping www.a.shifen.com (61.135.169.121) 56(84) bytes of data. ping github.com (192.30.255.112) 56(84) bytes of data. ping stackoverflow.com (151.101.193.69) 56(84) bytes of data. 64 bytes 61.135.169.121: icmp_seq=1 ttl=56 time=5.53 ms 64 bytes 151.101.193.69: icmp_seq=1 ttl=53 time=75.1 ms 64 bytes 192.30.255.112: icmp_seq=1 ttl=47 time=262 ms 64 bytes 61.135.169.121: icmp_seq=2 ttl=56 time=5.24 ms 64 bytes 151.101.193.69: icmp_seq=2 ttl=53 time=74.7 ms 64 bytes 192.30.255.112: icmp_seq=2 ttl=47 time=262 ms 64 bytes 61.135.169.121: icmp_seq=3 ttl=56 time=30.4 ms 64 bytes 151.101.193.69: icmp_seq=3 ttl=53 time=107 ms 64 bytes 192.30.255.112: icmp_seq=3 ttl=47 time=260 ms 64 bytes 61.135.169.121: icmp_seq=4 ttl=56 time=20.0 ms --- www.a.shifen.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3003ms rtt min/avg/max/mdev = 5.244/15.311/30.413/10.578 ms 64 bytes 151.101.193.69: icmp_seq=4 ttl=53 time=95.3 ms --- stackoverflow.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3005ms rtt min/avg/max/mdev = 74.732/88.154/107.341/13.857 ms 64 bytes 192.30.255.112: icmp_seq=4 ttl=47 time=256 ms --- github.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3003ms rtt min/avg/max/mdev = 256.064/260.310/262.474/2.606 ms
Comments
Post a Comment