PHP 7.4.0RC6 Released!

The parallel\Future class

(0.8.0)

Futures

A Future represents the return value or uncaught exception from a task, and exposes an API for cancellation.

Beispiel #1 Example showing Future as return value

<?php
$runtime 
= new \parallel\Runtime;
$future  $runtime->run(function(){
    return 
"World";
});
printf("Hello %s\n"$future->value());
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Hello World

The behaviour of a future also allows it to be used as a simple synchronization point even where the task does not return a value explicitly.

Beispiel #2 Example showing Future as synchronization point

<?php
$runtime 
= new \parallel\Runtime;
$future  $runtime->run(function(){
    echo 
"in child ";
    for (
$i 0$i 500$i++) {
        if (
$i 10 == 0) {
            echo 
".";
        }
    }
    echo 
" leaving child";
});

$future->value();
echo 
"\nparent continues\n";
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

in child .................................................. leaving child
parent continues

Klassenbeschreibung

final parallel\Future {
/* Resolution */
public value ( void ) : mixed
/* State */
public cancelled ( void ) : bool
public done ( void ) : bool
/* Cancellation */
public cancel ( void ) : bool
}

Inhaltsverzeichnis

add a note add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top