Les concepts de base de Beanstalkd, les méthodes de compilation et d'installation de yum ont été présentés dans l'article précédent "Explication détaillée de la file d'attente de messages/tâches Beanstalkd". Aujourd'hui, je vais pratiquer le processus d'utilisation de Beanstalkd avec PHP. . Le mieux est d'utiliser la classe Pheanstalk pour vous connecter à Beanstalkd
1 Utiliser Composer pour installer Pheanstalk
composer require pda/pheanstalk
2.
php Afficher le script d'état beanstalkd Status.php<?php /** * Created by PhpStorm. * User: jmsite.cn * Date: 2019/1/21 * Time: 10:32 */ require "../vendor/autoload.php"; use Pheanstalk\Pheanstalk; $pheanstalk = new Pheanstalk('192.168.75.135',11300); print_r($pheanstalk->stats());
<?php /** * Created by PhpStorm. * User: jmsite.cn * Date: 2019/1/20 * Time: 16:30 */ require "../vendor/autoload.php"; use Pheanstalk\Pheanstalk; $pheanstalk = new Pheanstalk('192.168.75.135',11300); for ($i=0;$i<50;$i++){ $data = array( 'key' => 'testkey'.$i, 'value' => 'testvalue', 'time' => time(), ); $ret = $pheanstalk->putInTube('test-tube', json_encode($data), Pheanstalk::DEFAULT_PRIORITY, Pheanstalk::DEFAULT_DELAY, Pheanstalk::DEFAULT_TTR); var_dump($ret); }
<?php /** * Created by PhpStorm. * User: jmsite.cn * Date: 2019/1/20 * Time: 16:31 */ set_time_limit(0); ini_set('default_socket_timeout', 900); require "../vendor/autoload.php"; use Pheanstalk\Pheanstalk; $pheanstalk = new Pheanstalk('192.168.75.135',11300); while (true){ $job = $pheanstalk ->watch('test-tube') ->ignore('default') ->reserve(); if ($job){ sleep(2); echo $job->getData(); echo "\n"; $pheanstalk->delete($job); } }
PS E:\repository\work\beanstalk> php .\Producter.php int(101) int(102) int(103) int(104) int(105) int(106) int(107) int(108) int(109) int(110) int(111) int(112) int(113) int(114) ......
PS E:\repository\work\beanstalk> php Status.php Pheanstalk\Response\ArrayResponse Object ( [_name:Pheanstalk\Response\ArrayResponse:private] => OK [storage:ArrayObject:private] => Array ( [current-jobs-urgent] => 0 [current-jobs-ready] => 50 [current-jobs-reserved] => 0 [current-jobs-delayed] => 0 [current-jobs-buried] => 0 ......
PS E:\repository\work\beanstalk> php .\Consumer.php {"key":"testkey0","value":"testvalue","time":1548039103} {"key":"testkey1","value":"testvalue","time":1548039103} {"key":"testkey2","value":"testvalue","time":1548039103} {"key":"testkey4","value":"testvalue","time":1548039103} {"key":"testkey6","value":"testvalue","time":1548039103} {"key":"testkey8","value":"testvalue","time":1548039103} {"key":"testkey10","value":"testvalue","time":1548039103} {"key":"testkey12","value":"testvalue","time":1548039103} {"key":"testkey14","value":"testvalue","time":1548039103} {"key":"testkey16","value":"testvalue","time":1548039103} {"key":"testkey18","value":"testvalue","time":1548039103} {"key":"testkey20","value":"testvalue","time":1548039103} {"key":"testkey22","value":"testvalue","time":1548039103} {"key":"testkey24","value":"testvalue","time":1548039103} {"key":"testkey26","value":"testvalue","time":1548039103} {"key":"testkey28","value":"testvalue","time":1548039103} {"key":"testkey30","value":"testvalue","time":1548039103} {"key":"testkey32","value":"testvalue","time":1548039103} {"key":"testkey34","value":"testvalue","time":1548039103} {"key":"testkey36","value":"testvalue","time":1548039103} {"key":"testkey38","value":"testvalue","time":1548039103} {"key":"testkey40","value":"testvalue","time":1548039103} {"key":"testkey42","value":"testvalue","time":1548039103} {"key":"testkey44","value":"testvalue","time":1548039103} {"key":"testkey46","value":"testvalue","time":1548039103} {"key":"testkey48","value":"testvalue","time":1548039103}
PS E:\repository\work\beanstalk> php .\Consumer.php {"key":"testkey3","value":"testvalue","time":1548039103} {"key":"testkey5","value":"testvalue","time":1548039103} {"key":"testkey7","value":"testvalue","time":1548039103} {"key":"testkey9","value":"testvalue","time":1548039103} {"key":"testkey11","value":"testvalue","time":1548039103} {"key":"testkey13","value":"testvalue","time":1548039103} {"key":"testkey15","value":"testvalue","time":1548039103} {"key":"testkey17","value":"testvalue","time":1548039103} {"key":"testkey19","value":"testvalue","time":1548039103} {"key":"testkey21","value":"testvalue","time":1548039103} {"key":"testkey23","value":"testvalue","time":1548039103} {"key":"testkey25","value":"testvalue","time":1548039103} {"key":"testkey27","value":"testvalue","time":1548039103} {"key":"testkey29","value":"testvalue","time":1548039103} {"key":"testkey31","value":"testvalue","time":1548039103} {"key":"testkey33","value":"testvalue","time":1548039103} {"key":"testkey35","value":"testvalue","time":1548039103} {"key":"testkey37","value":"testvalue","time":1548039103} {"key":"testkey39","value":"testvalue","time":1548039103} {"key":"testkey41","value":"testvalue","time":1548039103} {"key":"testkey43","value":"testvalue","time":1548039103} {"key":"testkey45","value":"testvalue","time":1548039103} {"key":"testkey47","value":"testvalue","time":1548039103} {"key":"testkey49","value":"testvalue","time":1548039103}
3. Choses à noter
1 Lors de la création d'un travail, le délai d'attente défini Pheanstalk::DEFAULT_TTR doit être plus long que le temps nécessaire au consommateur. pour traiter un travail, sinon le travail sera passé. Après l'expiration du délai, le tube passera à l'état prêt et sera obtenu par d'autres consommateurs. À ce moment-là, le consommateur actuel est toujours en train de traiter le travail, ce qui conduit au terrible phénomène d'un. travail exécuté à plusieurs reprises par plusieurs consommateurs 2. Le responsable de Pheanstalk a changé dans la nouvelle version de Pheanstalk, les connexions longues ne sont pas prises en charge lorsque le temps nécessaire au socket client pour se connecter au serveur dépasse le default_socket_timeout. défini dans php.ini, si le travail ne peut pas être obtenu à partir du tube du serveur, la connexion sera déconnectée, le processus consommateur doit donc être maintenu afin que le processus puisse être redémarré après la sortie. Il est recommandé d'utiliser superviseur pour maintenir le processus. processus de consommation. Code pour déterminer le délai d'expiration du socketpublic function getLine($length = null) { $timeout = ini_get('default_socket_timeout'); $timer = microtime(true); do { $data = isset($length) ? $this->_wrapper()->fgets($this->_socket, $length) : $this->_wrapper()->fgets($this->_socket); if ($this->_wrapper()->feof($this->_socket)) { throw new Exception\SocketException('Socket closed by server!'); } if (($data === false) && microtime(true) - $timer > $timeout) { $this->disconnect(); throw new Exception\SocketException('Socket timed out!'); } } while ($data === false); return rtrim($data); }
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!