Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126886 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 3547C1A00BC for ; Fri, 21 Mar 2025 10:17:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1742552092; bh=IFL8UOQr6Gw6Ee0wdIxLF3R6aJWOfvgZlLsIfTZL+e8=; h=From:Subject:Date:References:To:In-Reply-To:From; b=LuHjCz3W3MFPkNz+YJV3WXnbogPRCMAf8RfrVp5xhVnjqf8Eo4jgGTLSMgyFB3kq/ NpbsoAlcK3J5XzV40onWKF/aNYWoAqr5qpv3qom/SprQWCyXXEpSkJiBAdFTyXx8rX EW9qYpu5mMrS0SXQ4JYktoUZpi+qqUvhgY1/HCtSmLqaiJ3NU0hfNbk/OFWQDQQdu8 hhDqZkQI4YL6Z86BOg1FJgMTjqS91dv22et/trMTFNxew2+vcswjfZf1cE8HaVoXa2 Zu5NjNrrUDCW30pz5f+9ByeMnj9jnP5AUj08UYgCfZw6zlSV0BWXAX0zEgVv8pbUqH lnz8pZluBNscQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AD965180081 for ; Fri, 21 Mar 2025 10:14:51 +0000 (UTC) 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_50,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-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (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, 21 Mar 2025 10:14:51 +0000 (UTC) Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-5e5e8274a74so2786693a12.1 for ; Fri, 21 Mar 2025 03:17:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742552240; x=1743157040; darn=lists.php.net; h=message-id:in-reply-to:to:references:date:subject:mime-version :content-transfer-encoding:from:from:to:cc:subject:date:message-id :reply-to; bh=8tpaz2A2wRNrgWLA3nRNlATFoN67dsjntP1OavaM1QE=; b=Kd0Ctqlmn2ktONTKoAm+zP5qIX22FYAAIGtvZnzxI1+jOBoKpsyDOY4ojzPV738F37 QgEtVqRiCXGWlK8A4AweSxkG8nLSdnMiFVZTUAiOtwjvne5oguAaxdj49i5fDD+p+ZjI AsNI7VUzqRd1VI7GTDaJRxRDG/QAj1J0HdvgW+x+A5DGz8d3oHUGwNkIDv8yfBW6vpZf u9cLd4TxptlueLF0tvCSa9yN7SMwM2CD4QvNelRAP40B22a4I4gIl3rQmPfd9v/UlYRC hE6tStTYgKI64xXPqsgRDdXyYG5bKuP8q83TRh5+OL7JSOmA54Mq4J6VQ497Zf9frU5L x8iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742552240; x=1743157040; h=message-id:in-reply-to:to:references:date:subject:mime-version :content-transfer-encoding:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8tpaz2A2wRNrgWLA3nRNlATFoN67dsjntP1OavaM1QE=; b=H+uj5hjdRSVybcHc7/7V0qOCOkudcYeEdcZuJPZ+dP+O/4dlLlPe2/BIl6RX1KuJnr K+uB0Atf5SlGFahZ9glmu7UakEcs9rufjPBkT+4KL8FVBsZZgbcsJINSkVMamxlZqrHb FSy54s7+qAg91oFnaxUsi46HJRq9XzQINzzmjgXzrDR5pKmt1XjqIC7uKK+J2eNTM9bp 0Ph1HkiBnPHh1ZL+unVaVbY7q2MtkXMvRdak1SoF4ur2xLcMc0JdvB4RtBzK7IRj3XIf lvdYOoWNoTaUrYscnELeU0tL7xC4FtR0iG2fZ/7PM05wsxqx5iNpo/pG/LesmqctGTmr /6Vw== X-Gm-Message-State: AOJu0Yx9yrIK2DNZrDJeNfc8XaOom0tMVXkuUqpBLpJ/0aswf5YpGuuE EV9c4ZiNedVgoEfkrHlFxlwlD/BA2VFzK0eDE/qE6mDhAsz4zlACPuhMyQ== X-Gm-Gg: ASbGncsUWFi9cg5qPU3wqip08nTKcxJhneVFZk6vH42Re8dBHhmxCqRRPy4nUoFHeKO /QOBUJiMqSYCB/V3qjGEgpzEYTNg9ZwAzn5OiZO6ZesoBE8W8c7KALnSrp7gOrWXYR8z+JhfpeK t97xxtDHcNtMattM9qhY1oMqozP0POegAIUpOpxPXTwF6SpACSEVxhqhxZrsjLdKmwl4/47y7Dr qYszNYtzrRyMjTuwvJ1FTGi9Kpg3uNajT8IeuOjkSuWgmH4aVFi6wqjsVxRZ6bvJpGd8N3SpAgh Kgp0M+nK4CMIh9N+ZY1Cr9l0Y8Jzj4/Tgwtj2/j+kRbhV9EXZ8GsSVwkU7kbB+n2p/iHJywoOOH 6HSZtGPXjgTnsokBImAUgU+JZT+fMmq2d8EX2lZCEGrc9AiceD4k4dA== X-Google-Smtp-Source: AGHT+IFeJWbsVt97/vKvGJl7TI0EotUcgRDHMf3oHkLFb5j/iiBJhASdiKTV6hdpkRKFxd30KSdoKw== X-Received: by 2002:a05:6402:3549:b0:5eb:c90f:86ae with SMTP id 4fb4d7f45d1cf-5ebcd40aa88mr2381044a12.7.1742552240242; Fri, 21 Mar 2025 03:17:20 -0700 (PDT) Received: from smtpclient.apple (luna-da9c6750c4c3d2050000f.net.as198747.daniil.it. [2a0e:97c0:38f:0:502d:3c4c:576:c9ad]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ebccf85845sm1094128a12.21.2025.03.21.03.17.19 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Mar 2025 03:17:19 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.400.131.1.6\)) Subject: Re: [PHP-DEV] PHP True Async RFC - Stage 2 Date: Fri, 21 Mar 2025 11:17:08 +0100 References: To: internals@lists.php.net In-Reply-To: Message-ID: <0A5443F7-6C29-474A-A286-42A76FF9CB33@gmail.com> X-Mailer: Apple Mail (2.3826.400.131.1.6) From: daniil.gentili@gmail.com (Daniil Gentili) >=20 > As I write more code examples, I'm starting to get annoyed by the = verbosity of the `spawn in $scope` construct=E2=80=94especially in = situations where all spawns need to happen within the same context. >=20 > At the same time, in 80% of cases, it turns out that explicitly = defining `$scope` is the only correct approach to avoid shooting = yourself in the foot. =20 > So, it turns out that the `spawn in $scope` construct is used far more = frequently than a plain `spawn`. >=20 > with async >=20 > ```php > function generateReport(): void > { > try { > =20 > $scope =3D Scope::inherit(); > =20 > async $scope { > [$employees, $salaries, $workHours] =3D await Async\all([ > spawn fetchEmployees(), > spawn fetchSalaries(), > spawn fetchWorkHours() > ]); > =20 > foreach ($employees as $id =3D> $employee) { > $salary =3D $salaries[$id] ?? 'N/A'; > $hours =3D $workHours[$id] ?? 'N/A'; > echo "{$employee['name']}: salary =3D $salary, hours =3D= $hours\n"; > } =20 > } > =20 > } catch (Exception $e) { > echo "Failed to generate report: ", $e->getMessage(), "\n"; > } > } >=20 > ``` >=20 > #### async syntax >=20 > ```php > async { > > } > ``` >=20 I can see how you think that syntactic sugar is understandably needed = for spawn in scope, but again, you=E2=80=99re still writing code that = makes no sense: why do you care about fetchEmployees (a possible library = function) not spawning any fiber? You already explicitly await all fibers spawned in the generateReport = function, you get all the data you need, any extra spawned fibers should = not interest you for the purpose of the logic of generateReport. This is because again, the main use case listed of making sure all = fibers are done after a request is a footgun is a non-business-logic = requirement, an exercise in functional purity that also reduces caching = and concurrency opportunities, as mentioned before. A (somewhat bikesheeding, but this has been the vast majority of the = posts on this thread anyway) note is that await could also be made to = accept an iterable of futures, avoiding the use of Async\all = combinators. Regards, Daniil Gentili.=