PHP并发编程模型与Actor模式Actor模型是一种并发编程范式。每个Actor是一个独立的计算单元通过消息传递与其他Actor通信。今天说说PHP中Actor模式的实现。Actor的核心特征是封装状态、通过消息通信、单线程处理消息。phpclass Actor{private string $name;private array $state [];private array $mailbox [];private bool $processing false;private array $behaviors [];public function __construct(string $name, array $initialState []){$this-name $name;$this-state $initialState;}public function receive(string $message, mixed $data null): void{$this-mailbox[] [message $message, data $data];if (!$this-processing) {$this-process();}}public function on(string $message, callable $handler): void{$this-behaviors[$message] $handler;}private function process(): void{$this-processing true;while (!empty($this-mailbox)) {$envelope array_shift($this-mailbox);$handler $this-behaviors[$envelope[message]] ?? null;if ($handler) {try {$handler($envelope[data], $this);} catch (\Exception $e) {echo [Actor {$this-name}] 错误: {$e-getMessage()}\n;}}}$this-processing false;}public function setState(string $key, mixed $value): void{$this-state[$key] $value;}public function getState(string $key, mixed $default null): mixed{return $this-state[$key] ?? $default;}public function getName(): string{return $this-name;}public function getStateSnapshot(): array{return $this-state;}}class ActorSystem{private array $actors [];private array $deadLetters [];public function createActor(string $name, array $initialState []): Actor{$actor new Actor($name, $initialState);$this-actors[$name] $actor;return $actor;}public function getActor(string $name): ?Actor{return $this-actors[$name] ?? null;}public function send(string $to, string $message, mixed $data null): void{$actor $this-actors[$to] ?? null;if ($actor) {$actor-receive($message, $data);} else {$this-deadLetters[] [to $to, message $message, data $data];}}public function broadcast(string $message, mixed $data null): void{foreach ($this-actors as $actor) {$actor-receive($message, $data);}}public function getActorCount(): int{return count($this-actors);}public function getDeadLetters(): array{return $this-deadLetters;}}// 使用Actor模型实现订单处理系统$system new ActorSystem();$orderActor $system-createActor(order-processor, [orders []]);$orderActor-on(create, function ($data, $actor) {$order [id uniqid(ORD-),user $data[user],amount $data[amount],status created,created_at date(Y-m-d H:i:s),];$orders $actor-getState(orders);$orders[] $order;$actor-setState(orders, $orders);echo 订单已创建: {$order[id]}\n;});$orderActor-on(pay, function ($data, $actor) {$orders $actor-getState(orders);foreach ($orders as $order) {if ($order[id] $data[order_id]) {$order[status] paid;echo 订单已支付: {$order[id]}\n;break;}}$actor-setState(orders, $orders);});$orderActor-on(cancel, function ($data, $actor) {$orders $actor-getState(orders);foreach ($orders as $order) {if ($order[id] $data[order_id]) {$order[status] cancelled;echo 订单已取消: {$order[id]}\n;break;}}$actor-setState(orders, $orders);});$orderActor-on(stats, function ($data, $actor) {$orders $actor-getState(orders);$stats [total count($orders), paid 0, created 0, cancelled 0];foreach ($orders as $order) {$stats[$order[status]];}echo 订单统计: . json_encode($stats) . \n;});$logActor $system-createActor(logger, []);$logActor-on(log, function ($data) {echo [LOG] {$data[level]}: {$data[message]}\n;});// 发送消息$system-send(order-processor, create, [user 张三, amount 299.00]);$system-send(order-processor, create, [user 李四, amount 199.00]);$system-send(order-processor, stats, null);$system-broadcast(log, [level INFO, message 系统运行正常]);?Actor模型将状态封装在Actor内部通过消息传递实现通信。每个Actor处理消息时保证线程安全不需要锁。PHP中的Actor模型虽然不能利用多核CPU并发执行但提供了良好的代码组织和状态隔离机制。适合需要在单进程内管理复杂状态的场景。
PHP并发编程模型与Actor模式
PHP并发编程模型与Actor模式Actor模型是一种并发编程范式。每个Actor是一个独立的计算单元通过消息传递与其他Actor通信。今天说说PHP中Actor模式的实现。Actor的核心特征是封装状态、通过消息通信、单线程处理消息。phpclass Actor{private string $name;private array $state [];private array $mailbox [];private bool $processing false;private array $behaviors [];public function __construct(string $name, array $initialState []){$this-name $name;$this-state $initialState;}public function receive(string $message, mixed $data null): void{$this-mailbox[] [message $message, data $data];if (!$this-processing) {$this-process();}}public function on(string $message, callable $handler): void{$this-behaviors[$message] $handler;}private function process(): void{$this-processing true;while (!empty($this-mailbox)) {$envelope array_shift($this-mailbox);$handler $this-behaviors[$envelope[message]] ?? null;if ($handler) {try {$handler($envelope[data], $this);} catch (\Exception $e) {echo [Actor {$this-name}] 错误: {$e-getMessage()}\n;}}}$this-processing false;}public function setState(string $key, mixed $value): void{$this-state[$key] $value;}public function getState(string $key, mixed $default null): mixed{return $this-state[$key] ?? $default;}public function getName(): string{return $this-name;}public function getStateSnapshot(): array{return $this-state;}}class ActorSystem{private array $actors [];private array $deadLetters [];public function createActor(string $name, array $initialState []): Actor{$actor new Actor($name, $initialState);$this-actors[$name] $actor;return $actor;}public function getActor(string $name): ?Actor{return $this-actors[$name] ?? null;}public function send(string $to, string $message, mixed $data null): void{$actor $this-actors[$to] ?? null;if ($actor) {$actor-receive($message, $data);} else {$this-deadLetters[] [to $to, message $message, data $data];}}public function broadcast(string $message, mixed $data null): void{foreach ($this-actors as $actor) {$actor-receive($message, $data);}}public function getActorCount(): int{return count($this-actors);}public function getDeadLetters(): array{return $this-deadLetters;}}// 使用Actor模型实现订单处理系统$system new ActorSystem();$orderActor $system-createActor(order-processor, [orders []]);$orderActor-on(create, function ($data, $actor) {$order [id uniqid(ORD-),user $data[user],amount $data[amount],status created,created_at date(Y-m-d H:i:s),];$orders $actor-getState(orders);$orders[] $order;$actor-setState(orders, $orders);echo 订单已创建: {$order[id]}\n;});$orderActor-on(pay, function ($data, $actor) {$orders $actor-getState(orders);foreach ($orders as $order) {if ($order[id] $data[order_id]) {$order[status] paid;echo 订单已支付: {$order[id]}\n;break;}}$actor-setState(orders, $orders);});$orderActor-on(cancel, function ($data, $actor) {$orders $actor-getState(orders);foreach ($orders as $order) {if ($order[id] $data[order_id]) {$order[status] cancelled;echo 订单已取消: {$order[id]}\n;break;}}$actor-setState(orders, $orders);});$orderActor-on(stats, function ($data, $actor) {$orders $actor-getState(orders);$stats [total count($orders), paid 0, created 0, cancelled 0];foreach ($orders as $order) {$stats[$order[status]];}echo 订单统计: . json_encode($stats) . \n;});$logActor $system-createActor(logger, []);$logActor-on(log, function ($data) {echo [LOG] {$data[level]}: {$data[message]}\n;});// 发送消息$system-send(order-processor, create, [user 张三, amount 299.00]);$system-send(order-processor, create, [user 李四, amount 199.00]);$system-send(order-processor, stats, null);$system-broadcast(log, [level INFO, message 系统运行正常]);?Actor模型将状态封装在Actor内部通过消息传递实现通信。每个Actor处理消息时保证线程安全不需要锁。PHP中的Actor模型虽然不能利用多核CPU并发执行但提供了良好的代码组织和状态隔离机制。适合需要在单进程内管理复杂状态的场景。