協程使用注意事項
- 協程內部禁止使用全域變數,以免發生資料錯亂;
- 協程使用
use關鍵字引入外部變數到當前作用域禁止使用參考,以免發生資料錯亂; - 不能使用類靜態變數
Class::$array/ 全域變數$_array/ 全域物件屬性$object->array/ 其他超全域變數$GLOBALS等保存協程背景關系內容,以免發生資料錯亂; - 協程之間通訊必須使用通道(Channel);
- 不能在多個協程間共用一個客戶端連接,以免發生資料錯亂;可以使用連接池實作;
- 在 Swoole\Server 中,客戶端連接應當在 onWorkerStart 中創建;
- 在 Swoole\Process 中,客戶端連接應當在 Swoole\Process->start 后,子行程的回呼函式中創建;
- 必須在協程內捕獲例外,不得跨協程捕獲例外;
- 在
__get/__set魔術方法中不能有協程切換,
協程中的例外捕獲
示例一:協程中使用exit函式拋出 ExitException 例外
function route()
{
controller();
}
function controller()
{
your_code();
}
function your_code()
{
co::sleep(.001);
exit(1);
}
go(function () {
try {
route();
} catch (\Swoole\ExitException $e) {
var_dump($e->getMessage());
var_dump($e->getStatus() === 1);
var_dump($e->getFlags());
}
});
示例二:協程中使用exit函式拋出 ExitException 例外
$exit_status = 0;
go(function () {
try {
exit(123);
} catch (\Swoole\ExitException $e) {
global $exit_status;
$exit_status = $e->getStatus();
}
});
swoole_event_wait();
var_dump($exit_status);
示例三:協程中直接拋出 RuntimeException 例外
function test() {
throw new \RuntimeException(__FILE__, __LINE__);
}
Swoole\Coroutine::create(function () {
try {
test();
}
catch (\Throwable $e) {
echo $e;
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/5054.html
標籤:PHP
