Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129395 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 lists.php.net (Postfix) with ESMTPS id 20F5D1A00BC for ; Sat, 22 Nov 2025 14:37:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1763822267; bh=N2Tq2dqWrY94yqt3HkjnZ1tHI3n2NtDub7sC4AURvjE=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=QAJOcSLwEgD1ygM0zN6xd/RoaoCSWdXSjnrfzYNQs0idsB49SyDDvo8FNVWbEbxaV FhALK8cn+zTWQPA1AeziYp/2WmaCGv7YNopwebei1OENTdzdb9Y/wgFHiWecY1x/Ww GeXgSYPLhtajBkhQ/QHTzMyRoNT/sWy55AIUdUOvwOtTPcm4XSlLV6++AUbCwuCcUb HYHiI2QXbSkSaXN5/nQ8McRNQsuRfehEMfbpORGEQVrnkk+5xe80NJoGKcKmriSYMj V9rRwEdcYentCy4NP0TeQKMcnkVqG06FcSeu0bWLYXLhVQlWUaoMh81JoSjY3abEfa ZMIXIZidJnefA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 984AC180041 for ; Sat, 22 Nov 2025 14:37:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b4-smtp.messagingengine.com (fhigh-b4-smtp.messagingengine.com [202.12.124.155]) (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 ; Sat, 22 Nov 2025 14:37:46 +0000 (UTC) Received: from phl-compute-12.internal (phl-compute-12.internal [10.202.2.52]) by mailfhigh.stl.internal (Postfix) with ESMTP id 29C057A00F8; Sat, 22 Nov 2025 09:37:40 -0500 (EST) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-12.internal (MEProxy); Sat, 22 Nov 2025 09:37:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:cc:content-type:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm1; t=1763822260; x= 1763908660; bh=/LTnyAcibRg4RGCom2rol3BwIaUS9nP9ujJTXDuy91w=; b=m HCnCX5+Y/Tyj8Z/8hvC+BtWBjLV/nZrptuEBvEiEzaLdgHIMKcEi1JOxYA9FMatj LhuczUZ5B94q7coQ9OfyUc/ofncVRMm+alue3PXdYo3S7aEWavQt4QnnRmMHZdly V7kxb7y008c/ANFdnAu7Bhgz2gxhrVd67cG5l5g3a3j/Ff11rnStNszLa5janisU 0E0Gr0v1tjl0mHR/c1QIHX9rPGFJ3iLwp16K8Cq6I0NcP2voE6OqGOiBAu6AlPa4 BZYIpJ++RRLdNOPGQxhNSQUkyX1bflnei1kXvmUCOqjM7B53dU35sTZ5p4j7KP3F 8Qj6AGneG6q2YqSHgIKqw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1763822260; x=1763908660; bh=/LTnyAcibRg4RGCom2rol3BwIaUS9nP9ujJ TXDuy91w=; b=a75R2b1C6VXXkgRuIiWkVvNjLEo+04Jggil0DpwBw0LvzCDYhGg u5B2/X+Ey9f/OciELdj2dyiWKuiSomvHgNRaSS6ulp5cHwasdsRYue8NxiKvXA97 kYRQPAqc3dRzfYmVw3CR0Y/QOO8kWQTF9Y+Ljllc9QPQEmPUxSX1Og1C5zuXiB2K p60f5GrtxMliqy7/YvMVBQ/JHxwxPNUnVa/3cLaL1Zd8vt532XyNcLLXxQsqsIC7 j3lMDD2Tn2JYYp3u0ibM/qkRaQnhXcCEC2a4qJp8JiQoYRKkMfmbNnxmomD8AYjA /mwaX+mngOLMCoa+BxvXsbT/9lVl8GYagIw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggddvfeeftdelucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefoggffhffvvefkjghfufgtsegrtderre ertdejnecuhfhrohhmpedftfhosgcunfgrnhguvghrshdfuceorhhosgessghothhtlhgv ugdrtghouggvsheqnecuggftrfgrthhtvghrnhepieeuteehvddvfeejhffgieehleehhe dthfefkeejffelgfevvdekudetjeejtddtnecuvehluhhsthgvrhfuihiivgeptdenucfr rghrrghmpehmrghilhhfrhhomheprhhosgessghothhtlhgvugdrtghouggvshdpnhgspg hrtghpthhtohepfedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepvggumhhonhgu rdhhthesghhmrghilhdrtghomhdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsth hsrdhphhhprdhnvghtpdhrtghpthhtohepmhgrihhlshesthhhohhmrghssghlvgihrdgu vg X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 743951820074; Sat, 22 Nov 2025 09:37:39 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: AfNtV4qggFeZ Date: Sat, 22 Nov 2025 15:37:19 +0100 To: "Edmond Dantes" , "Thomas Bley" Cc: internals@lists.php.net Message-ID: <2e7ab8cc-a304-445c-9ef2-0449e6ce7873@app.fastmail.com> In-Reply-To: References: <383528143.397454.1763760932585@email.ionos.de> <691963327.399484.1763796022993@email.ionos.de> <1402906904.784903.1763811759896@email.ionos.de> <2015208655.784984.1763811978476@email.ionos.de> <980296152.1071.1763813573083@email.ionos.de> <92865666.4510.1763818506332@email.ionos.de> Subject: Re: [PHP-DEV] [VOTE] True Async RFC 1.6 Content-Type: multipart/alternative; boundary=aa7693f56e6048e093b20cc7330f6cde From: rob@bottled.codes ("Rob Landers") --aa7693f56e6048e093b20cc7330f6cde Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Sat, Nov 22, 2025, at 14:55, Edmond Dantes wrote: > > So I guess you want to use spawn() in a similar way as call_user_fun= c() works. > yes >=20 > > This changes the behavior of file_get_contents() from the outside > No. >=20 > ```php > function file_get_contents(string $filename): string > { > $fh =3D fopen(); >=20 > // It creates an EPOLL event so it can wake us when the data > becomes available. > $event =3D ReactorAPI.create_event_from($fh); > $waker =3D Scheduler.getCurrentWaker(); > // Event Driven logic inside. > $waker.add_event($event, function() use($waker) { > // Wakeup this coroutine > $waker.wake(); > }); >=20 > // suspend current coroutine > // zz..... z..... > Scheduler.suspend(); >=20 > // Continue here after the IO event >=20 > // Now we have date, return > return fread($fh, ....); > } > ``` >=20 > This is pseudocode. You can assume it always works. > If you call `file_get_contents` directly, it behaves the same way. > So it does not matter where `file_get_contents` is called. > Since all PHP code together with TrueAsync runs inside coroutines, > `file_get_contents` will suspend the coroutine in which it was invoked. >=20 > When you call `spawn`, you simply run the function in another > coroutine, not in your own. But `spawn` has no effect on > `file_get_contents`. >=20 > We=E2=80=99re not at risk of DataRace yet :) We don=E2=80=99t have mul= tithreading. > And most likely it won=E2=80=99t appear anytime soon. >=20 We are in data-race territory though: spawn(fn() =3D> file_put_contents('file', 'long string')) spawn(fn() =3D> file_get_contents('file')) I=E2=80=99m on a phone, so I=E2=80=99m not sure I got all the syntax rig= ht, but hopefully my intent is clear. But if these run =E2=80=9Cconcurre= ntly=E2=80=9D the scheduler will theoretically batch the reading/writing= of bytes in an interleaved way, causing absolute chaos and corruption. The same thing would happen with db drivers that typically use a token t= o keep track of which response goes to which request (I maintain an asyn= c db driver for amphp). There will be a need to ensure the stream cannot= be interleaved with other coroutines. I can do this with amphp locks, b= ut there isn=E2=80=99t even a semaphore implementation to build a lock a= round.=20 =E2=80=94 Rob --aa7693f56e6048e093b20cc7330f6cde Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable


On Sat, Nov 22, 2025, at 14:55, Edmond Dantes wrote:
> So I guess = you want to use spawn() in a similar way as call_user_func() works.
yes

> This changes the behavior of fil= e_get_contents() from the outside
No.

```php
function file_get_contents(string $filename): string
{
    $fh =3D fopen();

<= /div>
    // It creates an EPOLL event so it can wake= us when the data
becomes available.
  &nb= sp;  $event =3D ReactorAP= I.create_event_from($fh);
    $waker =3D&nb= sp;Scheduler.getCurrentWake= r();
    // Event Driven logic inside.
    $waker.add_eve= nt($event, function() use($waker) {
    &n= bsp;     // Wakeup this coroutine
 &n= bsp;        $waker.wake();
     });

    // suspend current coroutine
    // zz..... z.....
    = ;Scheduler.suspend();

    // Continue here after the IO event

    // Now we have date, return
    return fread($fh, ....);
}
```

This is pseudocode. You can assume it al= ways works.
If you call `file_get_contents` directly, it behav= es the same way.
So it does not matter where `file_get_content= s` is called.
Since all PHP code together with TrueAsync runs = inside coroutines,
`file_get_contents` will suspend the corout= ine in which it was invoked.

When you call `spa= wn`, you simply run the function in another
coroutine, not in = your own. But `spawn` has no effect on
`file_get_contents`.

We=E2=80=99re not at risk of DataRace yet :) We d= on=E2=80=99t have multithreading.
And most likely it won=E2=80= =99t appear anytime soon.


We are in data-race territory though:

sp= awn(fn() =3D> file_put_contents('file', 'long string'))
spa= wn(fn() =3D> file_get_contents('file'))

I=E2= =80=99m on a phone, so I=E2=80=99m not sure I got all the syntax right, = but hopefully my intent is clear. But if these run =E2=80=9Cconcurrently= =E2=80=9D the scheduler will theoretically batch the reading/writing of = bytes in an interleaved way, causing absolute chaos and corruption.

The same thing would happen with db drivers that ty= pically use a token to keep track of which response goes to which reques= t (I maintain an async db driver for amphp). There will be a need to ens= ure the stream cannot be interleaved with other coroutines. I can do thi= s with amphp locks, but there isn=E2=80=99t even a semaphore implementat= ion to build a lock around. 

=E2=80=94 Rob
--aa7693f56e6048e093b20cc7330f6cde--