Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64328 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 4534 invoked from network); 17 Dec 2012 22:45:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Dec 2012 22:45:09 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.170 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.217.170 mail-lb0-f170.google.com Received: from [209.85.217.170] ([209.85.217.170:42331] helo=mail-lb0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F5/C0-33799-370AFC05 for ; Mon, 17 Dec 2012 17:45:08 -0500 Received: by mail-lb0-f170.google.com with SMTP id j14so92648lbo.29 for ; Mon, 17 Dec 2012 14:45:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=JF8mnkKArf6TQ1wwOmSRP+2ckdLonTeKXhXnHGLq8pc=; b=Bfn8dU147qxe5fkAiIgSlVx9YIdtAxLo36oThkzZQXISK+kgJUz76Joh5SUGpZ6ATQ JFfajWe4IGGjUtZ2+PIr8m0OZzIQ8FUrTvBMoACjX76S3BO4aQsMbt15jqrELHhkBcIe 2du8HRMUaf8LPDKpaXYbRJIiOUjuGunqGDbzyCr+ErIBjW1f/oyo+q40g5Y9w4XDj3iU R3phzo8PWlsP/Ld3zxJvRwjxDEjPrUft5/60JJhgKGOYgGRPKBtBAdlYeFGqQPpjvn/N j/UvjkYv0Wdca2wPHwszn9iiCYofK9kstYcWzd2M8WPE/beUJgQ0jPYjbpWlyFRUutXz PE+A== MIME-Version: 1.0 Received: by 10.152.103.19 with SMTP id fs19mr12509799lab.15.1355784304634; Mon, 17 Dec 2012 14:45:04 -0800 (PST) Received: by 10.112.39.130 with HTTP; Mon, 17 Dec 2012 14:45:04 -0800 (PST) In-Reply-To: <50CF969D.8090707@sugarcrm.com> References: <50CF969D.8090707@sugarcrm.com> Date: Mon, 17 Dec 2012 23:45:04 +0100 Message-ID: To: Stas Malyshev Cc: PHP internals Content-Type: multipart/alternative; boundary=f46d040716dddea98c04d114210c Subject: Re: [PHP-DEV] Adding Generator::throw() From: nikita.ppv@gmail.com (Nikita Popov) --f46d040716dddea98c04d114210c Content-Type: text/plain; charset=ISO-8859-1 On Mon, Dec 17, 2012 at 11:03 PM, Stas Malyshev wrote: > Hi! > > > Basically the method allows you to do delegate error handling to the > > coroutine, rather than doing it yourself (as you are not always able to > do > > it). It is particularly useful in more complicated settings, e.g. if you > > are doing task scheduling through coroutines. For a small sample of how > > Could you expand on this point a bit more? It sounds like using > exceptions for flow control, which is usually a very bad idea. > > > this looks like see http://taskjs.org/. What the ->throw() method would > do > > in these examples is that it allows to check for errors by try/catching > the > > yield statement (rather than going for some odd solution with error > > callbacks). > > Could you point to some specific example? > The basic idea behind this kind of task management is that you can do asynchronous operations just like you would synchronous ones, i.e. without the need for callbacks or wait-loops. Whenever the functions wants to perform some async action it doesn't directly do it, rather it yields the operation that does it. The scheduler then waits until that operation is finished (running other tasks in the meantime) and only resumes the coroutine once it is finished. This way you can write async code without the ugliness that is usually involved with async code. Here is an example to get a rough idea of how the use looks like: function server() { // ... $stuff = yield $socket->recv(); // ... yield $socket->send($response); // ... } The throw() method comes in when you want to handle errors on those asynchronous operations. Without it you would be forced to use error codes. throw() allows you to do the error handling just like you would normally do. E.g. consider that $socket->send() were a fallible operation. Then you could catch errors like this: function server() { // ... try { yield $socket->send($response); } catch (SocketException $e) { // do some error handling } // ... } In this case the outside code (where the exception comes from) can't know what it should do in case of an error. Only the code in the coroutine knows that. That's why you need some possibility to throw the error into the context that knows how to deal with it. I hope that this makes it a bit more clear. Nikita --f46d040716dddea98c04d114210c--