Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / PHP

Vlákna Fiber a kooperatívne plánovanie

Fiber sa môže pozastaviť v hlbšom volaní a udržať zásobník.

Verzia: PHP 8.4; repository runtime, production runtime not attested

Syntax

Fiber::suspend($request); $fiber->resume($response);

Fiber sa môže pozastaviť v hlbšom volaní a udržať zásobník. Hodnota suspend ide volajúcemu a argument resume sa vnútri stane výsledkom suspend. Pri bežnom konci resume vráti null a getReturn výsledok. Ďalšie resume vyvolá FiberError.

function request(){return Fiber::suspend("ticket");}$f=new Fiber(fn()=>request()+1);$out=$f->start();$last=$f->resume(6);try{$f->resume();}catch(FiberError $e){$failure=get_class($e);}echo json_encode([$out,$last,$f->getReturn(),$failure]);
["ticket",null,7,"FiberError"]

Príklady

<?php
function request(){return Fiber::suspend("ticket");}$f=new Fiber(fn()=>request()+1);$out=$f->start();$last=$f->resume(6);try{$f->resume();}catch(FiberError $e){$failure=get_class($e);}echo json_encode([$out,$last,$f->getReturn(),$failure]);

Výsledok

["ticket",null,7,"FiberError"]

Zdroje