Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100343 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 15991 invoked from network); 1 Sep 2017 13:02:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Sep 2017 13:02:47 -0000 Authentication-Results: pb1.pair.com smtp.mail=i@lvht.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=i@lvht.net; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lvht.net from 54.207.22.56 cause and error) X-PHP-List-Original-Sender: i@lvht.net X-Host-Fingerprint: 54.207.22.56 smtpbgbr2.qq.com Received: from [54.207.22.56] ([54.207.22.56:47367] helo=smtpbgbr2.qq.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 83/3A-04538-F6A59A95 for ; Fri, 01 Sep 2017 09:02:40 -0400 X-QQ-mid: bizesmtp14t1504270951t92xrsb4 Received: from [192.168.1.4] (unknown [218.81.234.178]) by esmtp4.qq.com (ESMTP) with id ; Fri, 01 Sep 2017 21:02:30 +0800 (CST) X-QQ-SSF: 0130000000200010F330B00A0000000 X-QQ-FEAT: QX/rXDl9P1v7EcCtLH73I+r20vdkgXbWjGyLOOWaFRX43G4ZrM/kiJDH5jR5R TTAlgrmvJBXrmseHOq7S6f8rQ9L1DgAr2gnt7u0XloFGfSzYJwNlPycxgYNEoS6d40KMKQj x4FP8V0ZMB59mqzZTyl79HCIVJtNrub3hDoA24q8p4FEoXgXE9C/Ru/Wx3KIzD7urjN3498 kD+DJhxt+dzzTaKC+iY1Feylk7tefNNoLeJZjsLJm1LEcCIegWOGpXAB314Pfxk1sTT2gm7 Xdaw== X-QQ-GoodBg: 0 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) In-Reply-To: Date: Fri, 1 Sep 2017 21:02:29 +0800 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <8DB5D7F2-CB7A-4324-AB9F-D9934DD9C108@lvht.net> References: <7E991FBB-C115-4AB9-B904-EBE7C0F24089@lvht.net> To: Rowan Collins X-Mailer: Apple Mail (2.3273) X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:lvht.net:qybgforeign:qybgforeign4 X-QQ-Bgrelay: 1 Subject: Re: [PHP-DEV] add Fiber (sackful coroutine) support From: i@lvht.net (Haitao Lv) > On 1 Sep 2017, at 20:45, Rowan Collins = wrote: >=20 > Is this your own invention, or is the name and semantics based on some = existing language or computer science theory? "Fiber" makes me think of = strings, rather than coroutines, so maybe I'm missing a key metaphor = here. Fiber is a lightweight thread. Please see = https://en.wikipedia.org/wiki/Fiber_(computer_science) And ruby support Fiber. Please see = https://ruby-doc.org/core-2.4.1/Fiber.html >=20 >>> >> function foo($a) >>> { >>> $b =3D await $a + 1; >>> echo $b; >>> } >=20 > Should "await" be "yield" here? If not, what happens if I call foo() = without being inside a Fiber? No. A function with a yield will always return a Generator object. A = fiber with await will return any value. You can see the await as a resumable return. The value after it = will be returned and the function will be paused. >=20 > Similarly, if I run bar() and it runs foo(), what result will I get = outside a Fiber, since bar() has no yield, so is not itself a generator? >=20 >=20 >> So the Fiber API is a little like the Generator API, but is more = simple >> yet powerful. So there >> is no need to distinct $generator->current(), $generator->send(), and >> $generator->getReturn(). >=20 > Your ->resume() doesn't seem to do anything a normal generator can't, = except that the yield is nested deeper. The await is a nested deeper yield! > Nor does it seem to replace getReturn(). resume(); // will output 1 > Meanwhile, how does this relate to other ways of combining generators, = such as "yield from"? You can use Fiber to implement a more simple generator, because fiber = can be paused/resumed in its deeper call. resume(); // will output 1 echo $f->resume(); // will output 2 echo $f->resume(); // will output 3 echo $f->resume(); // will output 4 echo $f->resume(); // will output 5 echo $f->resume(); // will output 6