Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100335 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 50846 invoked from network); 31 Aug 2017 14:28:54 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Aug 2017 14:28:54 -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.254.200.128 cause and error) X-PHP-List-Original-Sender: i@lvht.net X-Host-Fingerprint: 54.254.200.128 smtpbgsg2.qq.com Received: from [54.254.200.128] ([54.254.200.128:32852] helo=smtpbgsg2.qq.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C3/C4-04538-E1D18A95 for ; Thu, 31 Aug 2017 10:28:48 -0400 X-QQ-mid: bizesmtp1t1504189718t030kdclp Received: from [192.168.1.4] (unknown [218.81.234.178]) by esmtp4.qq.com (ESMTP) with SMTP id 0 for ; Thu, 31 Aug 2017 22:28:37 +0800 (CST) X-QQ-SSF: 0130000000400010F230B00A0000000 X-QQ-FEAT: HrKUqYRgvQumkl3O1SmX4mc7dj4NrgiZs3PqNeGO2mcXTN6YyRp8+jah8j9GU ho2Ce1PvOy4R5vMOayCfv2B4UcMHhPp9Kiuo7eiyMYebD4hpKcknt2eTu9eCktIxAZpbmMy L43byZGclxAL22ydY32V86rvoFfuFwWyszkJWqr7H4vCfq3yqloXbUhFDdRvavYYZ6uqojb 5z/hsx8/mGGXfWErrkFuZlrHT9tivQjueQt+/7xRQ4JplwjuKs/lz6QnGz+TdLGY= X-QQ-GoodBg: 0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Message-ID: <7E991FBB-C115-4AB9-B904-EBE7C0F24089@lvht.net> Date: Thu, 31 Aug 2017 22:28:36 +0800 To: internals@lists.php.net X-Mailer: Apple Mail (2.3273) X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:lvht.net:qybgforeign:qybgforeign1 X-QQ-Bgrelay: 1 Subject: add Fiber (sackful coroutine) support From: i@lvht.net (Haitao Lv) Hi, All, The generator has been introduced in PHP 5.5. And we can it pause a = function execution. With these feature, we can schedule multi io task in userland without = blocking. More details can be found at [Nikic]. And there is also some asynchronous frameworks like [AMP], which allow = programer to=20 code like blocking, but run asynchronously. However, like python's generator, lua's coroutine, javascript's = async/await, php's generator also is a stackless coroutine implementation. We cannot pause = a function call in it's sub function call. This is the reason that frameworks like [AMP] = has many wrapper, is hard to understand, and almost impossible to be used to implement = real but complex system. So I propose to introduce the sackful coroutine, aka Fiber, support for = PHP. And the possible API like this, > function foo($a) > { > $b =3D await $a + 1; > echo $b; > } >=20 > function bar() > { > foo(); > } >=20 > $f =3D new Fiber(function($a) { > bar($a); > return 3; > }); >=20 > $c =3D $f->resume(1); > // 1 will be passed to lambda, then bar > // bar call foo, and the **await** in foo paused this execution > // and make the return value of resume as $a + 1 > // so $c is 2 >=20 > $c =3D $f->resume(3); > // resume the execution by previous await, and the $b in foo > // will be assigned a value of 3 > // so foo echo 3 and return and then the lambda return > // and the resume got a return value of 3. so $c is 3. 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(). I made a more complex example at [FIREPHP].And I also make a [PR] for = comment. All comment are welcome. Thanks. [Nikic] = https://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutin= es-in-PHP.html [AMP] http://amphp.org/ [FIREPHP] https://github.com/fiberphp/fiber-core/blob/master/app.php [PR] https://github.com/php/php-src/pull/2723=