<?php
$result = [];
(new swoole_mysql)->connect($conf, function ($db, $r) use (&$result) {
$result[] = 1;
});
(new swoole_mysql)->connect($conf, function ($db, $r) use (&$result) {
$result[] = 2;
});
How to judge that the above two asyncIOs have all been executed so that I can output $result?
<?php
$a = 1;
(new swoole_mysql)->connect($conf, function($db, $r) use (&a) {
$a = 2;
});
while ($a != 2)
continue;
echo $a;
Is there anything wrong with the above code? Why does echo $a
never execute?
A1:
Since non-blocking is used, it is better to use the business logic inside the callback
A2:
Since it is an asynchronous operation, the callback function may not be executed in which thread, so the context cannot be guaranteed. It is recommended to use coroutine syntax to do it.
Summary:
The questioner needs to understand swoole’s asynchronous model and don’t develop with synchronous thinking.