Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126676 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 CE9FF1A00BC for ; Sun, 9 Mar 2025 13:17:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1741526111; bh=Wj3vSDckbh1zBXuvRrOTRCRAvSGHwrfbwleByBd997o=; h=Date:Subject:To:References:From:In-Reply-To:From; b=LlxJk28TS4g1/ucadDHTfSCrbIsISBVmfY3XXCHLIN670Mjf1Hs1k3WcRjFDkbfDM lLLpH6F0VePfSysjEf7+iJHQBDEJk2B3prw8QIVf49OfpeXUWhYqmjlFa5OIXEZiSu clZra79WH4uHFm1hyPu4T1iHR7ERNTI6UKQl/l9a6qtNPmC3+o2bIkh3JQpCU+6DHB bomZU3I1WAWfGYJYztG+OukpvkKrhbO1aM/y56Z6+GGwloaW9uWQyFNQ7onebI4JZ2 DtLUZfx9ljujHX7LC5WHQbaoWirMKttPSJ4/ea/80i6eazrszzaEwv6AOskzNi0PVT EaGjDljEV43YA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 005D518004A for ; Sun, 9 Mar 2025 13:15:11 +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=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b7-smtp.messagingengine.com (fhigh-b7-smtp.messagingengine.com [202.12.124.158]) (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 ; Sun, 9 Mar 2025 13:15:10 +0000 (UTC) Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfhigh.stl.internal (Postfix) with ESMTP id 03971254013A for ; Sun, 9 Mar 2025 09:17:44 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-08.internal (MEProxy); Sun, 09 Mar 2025 09:17:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding: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=1741526264; x=1741612664; bh=ZD2FKEm8fALGnGiL7TQxhsgzO+ierpmIgV0UjGdsp64=; b= eUy/hdB6hPCZqp0JD9QdiJwg2GxM6X8XCWevWxHb6KpNKW9T1VC9wifSEhwzOKnn QuENh+BbCtpLVdYlTg97IwBcj+CFt2xtH/vTDvC8ZtBLN6z7OIYSf2OxMWoa21tb 82maYNq06srzkboJByJYSfTArrrHdbqJo/tpQwAsdnmIaKFoM2K6fFGrgIPFhYoh MLGfRgMdr6oaOf7KcPGFU0C80twcdqz5YWo7XqZvHlEoC81S2mE0uF85nqEZXUrx KQU4iwa/RvtewwgTBtrnCPoYWl4FK6BjOrr5LdEiVeAB1VC0ifC3Ctu0q5TwTyOM mTLfjmyyM8CAXXnhGxiFcQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding: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=fm1; t=1741526264; x=1741612664; bh=Z D2FKEm8fALGnGiL7TQxhsgzO+ierpmIgV0UjGdsp64=; b=CW3cBVRxtS+isfhHy lNC09oIkRUDJpoDXzhCtLUNAIIxkiWm6sZALBZ8KwljivNpq1AJ0IR4+tbjX6yCA lnrszCeoqO81dxxvs+KC/gTjBRgjIIyJIQwMX5QXAIMTGviGM/tASdiSyA2mLKlW iOCXMuidEuOT8YlyufRe8d3x6jL8s+qNBxO4hQWJRwBvfO07tt8QduV7m3YZ/LZo n5vBHUsna+VWHD+cUquXnH4niwPp88+S1HQWgADTNLsJ8SzejjiCLSUBnP40IazR A8v9B+ty4XJnLyFIW4LID39ofchN+Y1uQBDXcrGHFTbeZJuFzJTrb/HYZ/FYIj7f 1nIuQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduudeiheegucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpefkff ggfgfuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpedftfhofigrnhcuvfhomhhm ihhnshculgfkoffuohfrngdfuceoihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqe enucggtffrrghtthgvrhhnpeffkeevudffuddvheejvdefkeelfedtudegfeehjeduheeg ieduffeggeegveefheenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpehimhhsohhprdhphhhpsehrfigvtgdrtghordhukhdpnhgspghrtghpthht ohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslh hishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sun, 9 Mar 2025 09:17:43 -0400 (EDT) Message-ID: Date: Sun, 9 Mar 2025 13:17:43 +0000 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] Re: PHP True Async RFC To: internals@lists.php.net References: <1b6a5b3b-be9b-4e46-9cc6-b8b7f57b8494@app.fastmail.com> Content-Language: en-GB In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 08/03/2025 20:22, Edmond Dantes wrote: > > For coroutines to work, a Scheduler must be started. There can be only > one Scheduler per OS thread. That means creating a new async task does > not create a new Scheduler. > > Apparently, async {} in the examples above is the entry point for the > Scheduler. > I've been pondering this, and I think talking about "starting" or "initialising" the Scheduler is slightly misleading, because it implies that the Scheduler is something that "happens over there". It sounds like we'd be writing this: // No scheduler running, this is probably an error Async\runOnScheduler( something(...) ); Async\startScheduler(); // Great, now it's running... Async\runonScheduler( something(...) ); // If we can start it, we can stop it I guess? Async\stopScheduler(); But that's not we're talking about. As the RFC says: > Once the Scheduler is activated, it will take control of the Null-Fiber context, and execution within it will pause until all Fibers, all microtasks, and all event loop events have been processed. The actual flow in the RFC is like this: // This is queued somewhere special, ready for a scheduler to pick it up later Async\enqueueForScheduler( something(...) ); // Only now does anything actually run Async\runSchedulerUntilQueueEmpty(); // At this point, the scheduler isn't running any more // If we add to the queue now, it won't run unless we run another scheduler Async\enqueueForScheduler( something(...) ); Pondering this, I think one of the things we've been missing is what Unix[-like] systems call "process 0". I'm not an expert, so may get details wrong, but my understanding is that if you had a single-tasking OS, and used it to bootstrap a Unix[-like] system, it would look something like this: 1. You would replace the currently running single process with the new kernel / scheduler process 2. That scheduler would always start with exactly one process in the queue, traditionally called "init" 3. The scheduler would hand control to process 0 (because it's the only thing in the queue), and that process would be responsible for starting all the other processes in the system: TTYs and login prompts, network daemons, etc I think the same thing applies to scheduling coroutines: we want the Scheduler to take over the "null fiber", but in order to be useful, it needs something in its queue. So I propose we have a similar "coroutine zero" [name for illustration only]: // No scheduler running, this is an error Async\runOnScheduler( something(...) ); Async\runScheduler(     coroutine_zero: something(...); ); // At this point, the scheduler isn't running any more It's then the responsibility of "coroutine 0", here the function "something", to schedule what's actually wanted, like a network listener, or a worker pool reading from a queue, etc. At that point, the relationship to a block syntax perhaps becomes clearer: async {    spawn start_network_listener(); } is roughly (ignoring the difference between a code block and a closure) sugar for: Async\runScheduler(     coroutine_zero: function() {        spawn start_network_listener();    } ); That leaves the question of whether it would ever make sense to nest those blocks (indirectly, e.g. something() itself contains an async{} block, or calls something else which does). I guess in our analogy, nested blocks could be like running Containers within the currently running OS: they don't actually start a new Scheduler, but they mark a namespace of related coroutines, that can be treated specially in some way. Alternatively, it could simply be an error, like trying to run the kernel as a userland program. -- Rowan Tommins [IMSoP]