Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121798 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 46769 invoked from network); 24 Nov 2023 09:24:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Nov 2023 09:24:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6743E180040 for ; Fri, 24 Nov 2023 01:24:53 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-oi1-f171.google.com (mail-oi1-f171.google.com [209.85.167.171]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 24 Nov 2023 01:24:52 -0800 (PST) Received: by mail-oi1-f171.google.com with SMTP id 5614622812f47-3b2e72fe47fso1068400b6e.1 for ; Fri, 24 Nov 2023 01:24:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700817887; x=1701422687; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=33s5TO7HbQWK/LuFI6i80eGxgrJe3VMHuN8p/2n/dOc=; b=W4nGecKHuSPQZrLEBGFhehW1Rr3rELyMdh09UWyJmvAJS+8eVsWDKRQwf1/ai75A05 2eqyO1RxjPQp7gJ2cGb/elkjAMcczkTVXzdPLnhGqLmesblrXSI6pxpUzl7kJiLZQTYA QfuFzzaeA4hZxvO3Rx4qp1qpiHC/2+TQmtY1nyTQfmnGHOqvFvfWvdAUDz00AIGPj/gd pDzY3YSzNScFMEArXAK2LtdiDCCckPcba0rw4Tzit6bS6h5XvHyNEcsgbF2qexoUqjlh phjuJLPNppbLaiVVdRhzeGbOMGs1QXISaNtfLdcR4/fkKceM9XElOPtAgD648+ypIGRU wOwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700817887; x=1701422687; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=33s5TO7HbQWK/LuFI6i80eGxgrJe3VMHuN8p/2n/dOc=; b=TotSqtKeyp8d2Z/uIJI6vv2wFxVEprJpQmLvqojJsQEBFY8kYu9XTii0BCJz/9Nem5 F4s7EL505NyRkIIZG1UCIPxIS2ssSZC8wuMFHenQoJQq5UIPzL7y9KCHTEqF2N10duJv SEY3BYTfS1ESpoOl8zuH232jzlYNTSBCCWNYt04hSEqPz7Z3r5PAOkmlXOKbbwHrZ7cV 6SUNwlz2lbrCN2aO3gBdTKMr1r12tO2T8aps5vqtrGruu9URMS6YzULuJhp6DRFpArLc WXUBd5uM8HjSIZzKAlPMsw258na9f/bkkvuRWxK0eA4gppJMMYG3WVUUb1JUN6Xtf+1/ 1vow== X-Gm-Message-State: AOJu0YxctkoELJqadMYlw5ZpdyF0s7egEDFLuLagQb0laCxlfgK1j1Ah gXfwiesibYPWVo9a2HJssWxZ1/aLMLLDj65o2ag= X-Google-Smtp-Source: AGHT+IH3kzKvUWzVccgdsWVzqwEX1rP2Rx/gjTE3sMrH3M1gLCwoxYDphdS1kacv3WtXvl5Yf6NA5aKiDL4JSdXvxRk= X-Received: by 2002:a05:6808:1813:b0:3b5:a99c:5883 with SMTP id bh19-20020a056808181300b003b5a99c5883mr2665774oib.28.1700817887293; Fri, 24 Nov 2023 01:24:47 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 24 Nov 2023 10:24:36 +0100 Message-ID: To: chopins xiao Cc: "internals@lists.php.net" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Is it possible to add asynchronous loop call function ? From: landers.robert@gmail.com (Robert Landers) On Mon, Nov 20, 2023 at 10:06=E2=80=AFAM chopins xiao wrote: > > Similar to a tick event, an async function will be called automatically a= fter 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 conti= nue 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 =3D 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