Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126884 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 52B801A00BC for ; Fri, 21 Mar 2025 10:04:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1742551291; bh=snc9ul889FIN1yrTE4Dnga9Yh74KqahizERjSAj+Zfk=; h=References:In-Reply-To:From:Date:Subject:To:From; b=ZpaCBrAWXf/RbbN8jrvwzvh19kGY6msC68Gr0e/TsiUQHKrVWd+mhw21Fnb8zb0Mk ZMYz0WZcYRZDFEvaNPJsOhh7/DlynjVm6TLLubBIeO9lNwmT6D3Wsqhf7zXKNx2v5m /1VZswNYS+gYm/J1CsUHoV+FKMBeiR0612QcJQO649omKUkjnIE7F6awtUenh9EPJN eOxObAkRF5VFFN859gDvJaSHonDSGZNR7IMrUR2yv/d3B4TmFlOdf4PZkXAae0E6uG 2jqmb7+ASqUo8DcPcF6ZvtHx6vhMZ4Ziitfo9v2LDPEMt08Stf4FQph9nQu6910Inw +QXQjK2K4pXGw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7BC64180072 for ; Fri, 21 Mar 2025 10:01:30 +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, HTML_MESSAGE,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-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) (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:01:30 +0000 (UTC) Received: by mail-yb1-f170.google.com with SMTP id 3f1490d57ef6-e589c258663so1582761276.1 for ; Fri, 21 Mar 2025 03:04:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742551440; x=1743156240; darn=lists.php.net; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=54FA4JYhDtHxYfIuimXAxlnsFrIVxwpkM+SltmyLaHU=; b=LojfMKPVGDqSQui7nHFfBA3yogtpx/K/OXwhydI3M1WUfshDg+E2zDQrnvmXB8RI4U 7w4vzTpgT0eW/XSwTG7fRaCtD+5nXaralYn5q+21fpYrapYIRP5Y8rT3oeWREzmyBm6+ bdtzXixrLVPnwkX2CIYawI/BZTACd/WMG6aa6hhHXobAqBo1QGVjYVsT4dBuxsCQR/Fe XmC4MKOijiUwL1tyvcMVkPcc3O1r4a794Viav6AuDN/GxnmeJ3jqO7h8zF+cyDhDFMAB aIFde9vH0+LxDFa23+ycoVWtTAUFAx7h0ke+D0sQ4KDNBdZLaFwgrOsfgy3oQafmoF4N wyoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742551440; x=1743156240; h=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=54FA4JYhDtHxYfIuimXAxlnsFrIVxwpkM+SltmyLaHU=; b=iaAiQbsre3Szw9Gwv1eaoEwaf7fw9O0ZtPf1o3NH4K1L4LPLJDOH6jmfLPnn1N615c EYPRp0IP2M4ozyCqJwFR+77nraTdjdeWoV7yaAS4jYNMXrMFFTUREJnZJuFhCfF6+FCB Eg1y/xGZYa8l1NdYgfDZskpo4axS/zmwGMFwRPuwyRTh9p/ecxuEtimpwQ3whsNfNW+K Y3TnrA6beRzh12gH8OWas/i7SfyvWFWZEgGDtJ1bh7Zk56vqcvJX0xbdsmWnZKelCQTR 5Bl32vhHmKRGIaKVpqAhKsEREYpLHK43RMK4OoonMr7KQ+f7lghJxX8J8uEiwlM2ScPM ecTg== X-Gm-Message-State: AOJu0YxfmokmDefygmSFlMhPXt24PEUHyU81xz/evLaorvqACRYNV4pF 5IL/MSjFoeaxZoKbYLxTuRY3paJFMtft16ATaCOSFjw/YqFMubYYQeuGG6npIPT+zyQrSgeWSVb sZMElXcGDOKNVRgVJAB7a9pvfI1H6yca8kE4= X-Gm-Gg: ASbGncvTc18YSb+o70dFmROH/ncQHwRqGDVKJ9cHc9Qg83xIiZvUlJbWxmEtBzNa12G 2mPW8zT1YUWlMALrhotCdxOsaPMLsZxZ9cUyOIsU8pMsj+V4uco7c09ZQRx5g12Dgf17njxtIaS DyelRfJRGwHqjP6gl6N4WoKkuso/dnvDJdAIjM X-Google-Smtp-Source: AGHT+IEcoYMqB0zmRlMHF4u4T9toaVkSq/fxFvRI0Ibms6ZwZLhsFoDWUfk7x8Qj2vlZyfG3PTVf7YyJmdGAqv93G94= X-Received: by 2002:a05:6902:2504:b0:e58:ac80:3492 with SMTP id 3f1490d57ef6-e66a4d42cdamr2810449276.9.1742551439782; Fri, 21 Mar 2025 03:03:59 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 21 Mar 2025 12:03:50 +0200 X-Gm-Features: AQ5f1JpAMJgXH1IJT6bp98krE6wSt1bEFoazQRxi2KogzFGRINUwH35YUTXrC-Q Message-ID: Subject: [PHP-DEV] Re: PHP True Async RFC - Stage 2 To: php internals Content-Type: multipart/alternative; boundary="000000000000923da50630d75f3f" From: edmond.ht@gmail.com (Edmond Dantes) --000000000000923da50630d75f3f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Good day, everyone. 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 a= ll spawns need to happen within the same context. 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. So, it turns out that the `spawn in $scope` construct is used far more frequently than a plain `spawn`. I remembered an example that Larry came up with and decided to use it as syntactic sugar. There=E2=80=99s some doubt because the actual gain in characters is minimal= . This block doesn't change the logic in any way. The convenience lies in the fact that within the block, it=E2=80=99s clear = which `$scope` is currently active. However, this is more about visual organization than logical structure. Here's what I ended up with: ### Async blocks Consider the following code: ```php function generateReport(): void { $scope =3D Scope::inherit(); try { [$employees, $salaries, $workHours] =3D await Async\all([ spawn in $scope fetchEmployees(), spawn in $scope fetchSalaries(), spawn in $scope fetchWorkHours() ]); 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"; } } catch (Exception $e) { echo "Failed to generate report: ", $e->getMessage(), "\n"; } } ``` with async ```php function generateReport(): void { try { $scope =3D Scope::inherit(); async $scope { [$employees, $salaries, $workHours] =3D await Async\all([ spawn fetchEmployees(), spawn fetchSalaries(), spawn fetchWorkHours() ]); 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"; } } } catch (Exception $e) { echo "Failed to generate report: ", $e->getMessage(), "\n"; } } ``` #### async syntax ```php async { } ``` --000000000000923da50630d75f3f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Good day, everyone.=C2=A0=C2=A0

<= div>As I write more code examples, I'm starting to get annoyed by the v= erbosity of the `spawn in $scope` construct=E2=80=94especially in situation= s where all spawns need to happen within the same context.

At the sa= me time, in 80% of cases, it turns out that explicitly defining `$scope` is= the only correct approach to avoid shooting yourself in the foot. =C2=A0So, it turns out that the `spawn in $scope` construct is used far more fr= equently than a plain `spawn`.

I remembered an example that Larry ca= me up with and decided to use it as syntactic sugar. =C2=A0

<= /div>
There=E2=80=99s some doubt because the actual gain in characters = is minimal. This block doesn't change the logic in any way. =C2=A0
T= he convenience lies in the fact that within the block, it=E2=80=99s clear w= hich `$scope` is currently active. =C2=A0
However, this is more about vi= sual organization than logical structure.

Here's what= I ended up with:

### Async blocks

Cons= ider the following code:

```php
function generateReport(): void{
=C2=A0 =C2=A0 $scope =3D Scope::inherit();

=C2=A0 =C2=A0 try = {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 [$employees, $salaries, $workHours] =3D aw= ait Async\all([
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 spawn in $scop= e fetchEmployees(),
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 spawn in $= scope fetchSalaries(),
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 spawn i= n $scope fetchWorkHours()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 ]);

=C2=A0 = =C2=A0 =C2=A0 =C2=A0 foreach ($employees as $id =3D> $employee) {
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 $salary =3D $salaries[$id] ?? 'N= /A';
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 $hours =3D $workHours= [$id] ?? 'N/A';
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 echo &= quot;{$employee['name']}: salary =3D $salary, hours =3D $hours\n&qu= ot;;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

=C2=A0 =C2=A0 } catch (Excepti= on $e) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 echo "Failed to generate repor= t: ", $e->getMessage(), "\n";
=C2=A0 =C2=A0 }
}
= ```

with async

```php
function generateReport(): void
{=
=C2=A0 =C2=A0 try {
=C2=A0 =C2=A0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 $s= cope =3D Scope::inherit();
=C2=A0 =C2=A0 =C2=A0 =C2=A0
=C2=A0 =C2=A0= =C2=A0 =C2=A0 async $scope {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = [$employees, $salaries, $workHours] =3D await Async\all([
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 spawn fetchEmployees(),
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 spawn fetchSalaries(),=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 spawn fetchWorkHou= rs()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ]);
=C2=A0 =C2=A0
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 foreach ($employees as $id =3D>= ; $employee) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 $= salary =3D $salaries[$id] ?? 'N/A';
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 $hours =3D $workHours[$id] ?? 'N/A';=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 echo "{$emplo= yee['name']}: salary =3D $salary, hours =3D $hours\n";
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 } =C2=A0 =C2=A0 =C2=A0 =C2=A0
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0
=C2=A0 =C2=A0= } catch (Exception $e) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 echo "Failed = to generate report: ", $e->getMessage(), "\n";
=C2=A0 = =C2=A0 }
}

```

#### async syntax

```php
async &l= t;scope> {
=C2=A0 =C2=A0 <codeBlock>
}
```
=
--000000000000923da50630d75f3f--