3

This code:

$ip1 = `/usr/bin/dig $host1`;
$ip2 = `/usr/bin/dig $host2`;
$ip3 = `/usr/bin/dig $host3`;

run one by one. The problem is the dig response speed is random. It could be fast or it could be more than 10 sec. My question is how can I run this 3 line of code simultaneously on the same page?

I'm willing to accept other approach like running in 3 different pages. I have an array_chunk($input_array, 3). $input_array is a result from mysql query that contains value which I could run in 3 different lookup above. I split the query into 3 parts. Now the problem is I don't know how to send the 3 parts to 3 different pages and run the dig lookup. I only know how to do it with form. That's why I shown you the code above. I'm blank right now.

Please help me. Thanks in advance.

Brad
  • 159,648
  • 54
  • 349
  • 530
sg552
  • 1,521
  • 6
  • 32
  • 59
  • 1
    @MarkBaker [Backticks run shell commands](http://php.net/manual/en/language.operators.execution.php), of which [`/usr/bin/dig`](http://linux.die.net/man/1/dig) is one. – Powerlord Feb 17 '12 at 17:58
  • I stand corrected - my new bit of learning for the day – Mark Baker Feb 17 '12 at 18:02
  • What would you prefer your application be doing during those 10 seconds? – webbiedave Feb 17 '12 at 18:17
  • @webbiedave: That is just example. I could have like 100 or more nameserver to lookup. Instead of lookup 1 by one I was thinking maybe split it into 10 parts and run 10 dig lookup simultaneously. That will be faster than running 100 lookup on a single dig query. – sg552 Feb 17 '12 at 18:28

5 Answers5

1

This is not multithreading, also, you´re trying to run 3 lookups on domain name servers, and depending on each host, caching time of your NS server or hosts files, the speed can vary.

But if I understand, you can achieve what you want using shell batching:

$ dig uol.com.br; dig aol.com; dig nytimes.com

or even better, because dig support it:

$ dig uol.com.br aol.com nytimes.com

This will bring 3 lookups at once using just one command line, concatenated with ';';

To implement this use an array:

$hosts[] = 'aol.com';
$hosts[] = 'uol.com.br';
$hosts[] = 'nytimes.com';

passthru('dig '.implode(' ',$hosts));

Hope that helps

Guilherme Viebig
  • 6,901
  • 3
  • 28
  • 30
1

There is a couple of ways.

  1. Write small php script which is just returning one ip and updating in database. Run these processes with exec(). In main php program wait until all processes statuses will be "finished" in database. Get all results from db. Note: use nohup parameter to run process in background.

  2. use php pcntl to achieve your goal.

simon
  • 1,405
  • 1
  • 15
  • 24
0

Try the idea provided in the comment second to top:

Does PHP have threading?

PHP doesn't have multi-threading as far as I know, but this is a way to run each as a background process.

Community
  • 1
  • 1
RJ Cuthbertson
  • 1,458
  • 1
  • 20
  • 36
  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Chris S Aug 29 '12 at 21:52
0

Well, if it runs in a page, you could also try to use Ajax, using some javascript library it can be accomplished very easily

here's the documentation for ajax usage in jquery

http://api.jquery.com/category/ajax

DaneoShiga
  • 1,007
  • 8
  • 16
0

you can run shell commands in the background as long as you use exec(), and point the third argument to a file or other output stream, otherwise PHP will wait for the command to finish running before running the next line of code.

http://php.net/manual/en/function.exec.php

dqhendricks
  • 19,030
  • 11
  • 50
  • 83