Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112581 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27845 invoked from network); 22 Dec 2020 00:20:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Dec 2020 00:20:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E3FC91804E1 for ; Mon, 21 Dec 2020 15:53:13 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,HTML_MESSAGE, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mercury.negativeion.net (mercury.negativeion.net [199.38.81.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 21 Dec 2020 15:53:13 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mercury.negativeion.net (Postfix) with ESMTP id D76E120BC29454; Mon, 21 Dec 2020 18:53:12 -0500 (EST) Received: from mercury.negativeion.net ([127.0.0.1]) by localhost (mercury.negativeion.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 4ccbp01Kzk1j; Mon, 21 Dec 2020 18:53:12 -0500 (EST) Received: from [10.0.1.2] (unknown [173.225.157.176]) by mercury.negativeion.net (Postfix) with ESMTPSA id D261420BC2941F; Mon, 21 Dec 2020 18:53:11 -0500 (EST) Message-ID: <08E75593-0599-48B1-9C2D-D920EE942BAD@trowski.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_2C988518-CFD7-4CDC-964F-204014EE6ACD" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Date: Mon, 21 Dec 2020 17:53:10 -0600 In-Reply-To: Cc: php internals To: Mike Schinkel References: X-Mailer: Apple Mail (2.3608.120.23.2.4) Subject: Re: [PHP-DEV] [RFC] Fibers From: aaron@trowski.com (Aaron Piotrowski) --Apple-Mail=_2C988518-CFD7-4CDC-964F-204014EE6ACD Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Dec 21, 2020, at 4:33 PM, Mike Schinkel = wrote: >=20 >> On Dec 17, 2020, at 11:30 AM, Aaron Piotrowski = wrote: >>=20 >> Hello everyone! >>=20 >> I would like to introduce an RFC for adding full-stack fibers to PHP: = https://wiki.php.net/rfc/fibers >>=20 >> Fibers are primarily used to implement green-threads or coroutines = for asynchronous I/O. Fibers are similar to threads, except fibers exist = within a single thread and require cooperative scheduling of the fibers = by the process. Since fibers do not require a full CPU context switch, = they are lightweight and more performant than multi-processing or = threading for awaiting I/O. >>=20 >> An implementation as an extension is at = https://github.com/amphp/ext-fiber >>=20 >> Fibers are a complex feature. The RFC contains many examples and = links to code using fibers to help explain and demonstrate what is = possible, however I=E2=80=99m certain many more questions and concerns = will arise. Looking forward to feedback and discussion. >>=20 >=20 > This is interesting, and potentially very useful. >=20 > I am curious about how you propose access to shared memory across = fibers? What will happen if two fibers try to update a $GLOBALS = variable at the same time? Or a property of the same object? How will = developers manage that? >=20 > -Mike >=20 > P.S. Have you considered concurrency functionality like in GoLang[1] = e.g. channels, where the mantra is "Do not communicate by sharing = memory; instead, share memory by communicating?" >=20 > [1] = https://medium.com/@thejasbabu/concurrency-in-go-e4a61ec96491#:~:text=3DDo= %20not%20communicate%20by%20sharing,race%20conditions%2C%20memory%20manage= ment%20etc = . Hi Mike, Fibers do not change the single-threaded nature of PHP. Only a single = fiber can be running at one time, so memory cannot be modified = simultaneously. There are synchronization issues when writing asynchronous code using = either stackless or stackful coroutines, as anyone who has worked with = AMPHP or ReactPHP can tell you. Multiple fibers (coroutines, = green-threads, whatever you want to call them) will interleave = execution. Multiple interleaved fibers can change object state between = pausing and resuming, which I'm guessing is more to what you were = concerned about, rather than literal simultaneous modification that can = occur with threads. The RFC does not provide tools to synchronize memory = access, as these can be implemented in user space. Fibers don't provide = the entire API, just the "bare-metal" API needed to implement various = styles of concurrency in user space code. AMPHP provides synchronization tools such as mutexes, semaphores, = parcels, and channels in https://github.com/amphp/sync = and https://github.com/amphp/parallel = . Other libraries could provide = similar tools, though perhaps with a different approach. We too like the = Go-style of concurrency and look to emulate it in AMPHP v3. Cheers, Aaron Piotrowski --Apple-Mail=_2C988518-CFD7-4CDC-964F-204014EE6ACD--