Created
June 8, 2017 17:32
-
-
Save wallacemaxters/a2712a2f353703726b2bfbcb315cc68b to your computer and use it in GitHub Desktop.
Ótima forma de se trabalhar com filas/semáforos em PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Exemplo de Message Queue nativo em PHP | |
// Veja a documentação sobre "Funções Semáforo" : http://php.net/manual/pt_BR/ref.sem.php | |
define('__ID__', 59544); | |
// Definimos um ID para a Fila | |
$msg = msg_get_queue(__ID__); | |
// Aqui definimos um objeto que vai ser enviado para fila | |
$obj = (object) ['id' => uniqid(), 'nome' => 'Wallace']; | |
// Enviamos para a fila | |
msg_send($msg, 1, $obj); | |
var_dump($msg); | |
// Aquí dá pra ver algumas informações legais sobre a fila | |
var_dump(msg_stat_queue($msg)); | |
// se você atualizar a pagina, vai adicionando mais coisas à fila | |
// rode o "php worker.php" na linha de comando agora :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Definimos o ID igual ao de Index.php | |
define('__ID__', 59544); | |
$queue = msg_get_queue(__ID__); | |
$msg_type = NULL; | |
$msg = NULL; | |
$max_msg_size = 512; | |
// Processa tudo que é recebido da fila e fica em espera, aguardando mais ;) | |
while ($d = msg_receive($queue, 1, $msg_type, $max_msg_size, $msg)) { | |
var_dump($d); | |
print_r($msg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment