0


is there a good way to make some PHP calls asynchronous, non-blocking? For example, take a look at this simple code:

<?php
$hosts = [...]  // array of 100+ hosts
foreach ($hosts as $host){
  $sysNames['$host'] = snmpget($host, 'community', "system.sysName.0");
}
echo 'done'

If, for example, 10 hosts are down, that will make a huge delay.
How to make snmpget calls non-blocking?

I've tried with React\Promise but I couldn't found some useful examples to start from. Can anyone suggest proper implementation of that class?

Boban P.
  • 183
  • 2
  • 5
  • I think with async calls you wont be able to store the value in `$sysNames['$host']` by it's very nature you wont be waiting for the response, therefore there's likely no return data. Or none that would be useful – ArtisticPhoenix Jul 26 '15 at 07:22
  • I'm not aware of that class, but I use queues for all my long running processes to make them non blocking – baao Jul 26 '15 at 07:24
  • @michael https://github.com/reactphp/promise – Boban P. Jul 26 '15 at 07:49
  • Is it ok to call snmpget in separate thread in this scenario? Since threads share memory with parent process, it would be possible that thread return value to $sysNames variable, no? If that's true how to tell last echo to wait all threads to finish? – Boban P. Jul 26 '15 at 07:52

1 Answers1

0

PHP supports multi-threading using the threads extensions; but it needs a properly build php-binary, plus some additional dll's if you're on Windows

Sjon
  • 4,989
  • 6
  • 28
  • 46