/** * Warning: remember it's PHP. You need to use OS primitives for communicating with caller. * * @param array $array * @param callable $callable */ function parallel_each($array, $callable) { $count = count($array); $cores = hw_core_get(); $childs = array(); if ($count <= $cores) { foreach ($array as $k => $v) { $childs[] = async_call(function () use($k, $v, $callable) { $callable($v, $k); }); } } foreach (array_chunk($array, ceil($count / $cores), true) as $chunk) { $childs[] = async_call(function () use($chunk, $callable) { foreach ($chunk as $k => $v) { $callable($v, $k); } }); } foreach ($childs as $child) { $child['finish'](); } }
function wh_checkpoint() { if (queue_empty('webhook')) { return false; } return async_call('/admin/webhook.php'); }
<?php require_once 'functions.php'; if (is_direct_called(__FILE__)) { //特定的异步方式来触发 if (async_call()) { wh_handler(); exit; } //直接使用shell来触发:php -q webhook.php crontab if (isset($argv)) { if ($argv[1] === 'crontab') { wh_handler(); exit; } } //使用外部url来处罚 $req = get_param(); switch ($req['cmd']) { case 'crontab': wh_handler(); exit; case 'trigger': wh_trigger_exit(@$req['db'], @$req['table'], @$req['event'], @$req['data']); } } function wh_trigger_exit($db, $table, $event, $data) { denies_with_json(); if (!in_array($event, array('update', 'refresh'))) { jsonp_nocache_exit(array('status' => 'error', 'error' => 'invalid event type'));