PHP 8.5.0 Alpha 4 available for testing

Voting

: three minus zero?
(Example: nine)

The Note You're Voting On

gam6itko
3 years ago
<?php

// this example shows how Events handle Future events

use parallel\{Events, Events\Event, Runtime};

$events = new Events();

$runtime = new Runtime();

//Read (type: 1)
$future = $runtime->run(
static function (
string $name) {
return
"Future#$name result";
},
[
'Read']
);
$events->addFuture("Future#Read", $future);

//Cancel (type: 4)
$future = $runtime->run(
static function (
string $name) {
throw new
\Exception("Exception#$name");
},
[
'Cancel']
);
$events->addFuture("Future#Cancel", $future);

//Kill (type: 5)
$future = $runtime->run(
static function () {
sleep(100000);
},
[]
);
$events->addFuture("Future#Kill", $future);
$future->cancel(); //kill it

//Error (type: 6)
$future = $runtime->run(
static function () {
$memoryEater = [];
$i = 0;
while (++
$i) {
$memoryEater[] = $i;
}
}
);
$events->addFuture("Future#Error", $future);

// reading events

/** @var Event $event */
foreach ($events as $i => $event) {
echo
str_pad('', 50, '=') . " EVENT_$i\n";
var_dump($event);
echo
"\n";
}

<< Back to user notes page

To Top