Re: Is it possible to add asynchronous loop call function ?
| From: | Robert Landers | Date: | Fri, 24 Nov 2023 09:24:36 +0000 |
| Subject: | Re: Is it possible to add asynchronous loop call function ? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-121798@lists.php.net to get a copy of this message | ||
On Mon, Nov 20, 2023 at 10:06 AM chopins xiao <chopins.xiao@gmail.com> wrote:
>
> Similar to a tick event, an async function will be called automatically after the block of code
> .
> and the async function is cleared when script shutown.
> Async function can be cleared after they have returned, or they can continue to be called at
> the next event after they have returned.
> and some of the features of Fiber
>
I think you want a flattened
try {
// do stuff
} finally {
// clean up
}
You can also emulate this with:
class Defer {
private function __construct(private \Closure $callback) {}
public function __destruct() { $this->callback(); }
public static function _(\Closure $callback) { return new self($callback); }
}
and use it like:
function writeSomeStuff() {
// open files
$deferred = Defer::_($closeFiles(...));
// do stuff
}
So long as a reference exists to $deferred variable, the deferred
method won't be run. If the variable is local to the method/function
being run it, the deconstructor will be called after the function
returns.
It isn't the most beautiful thing in the world, and easy to forget to
store the result of Defer, but it is handy sometimes.
Robert Landers
Software Engineer
Utrecht NL