Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64412 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 56737 invoked from network); 21 Dec 2012 14:54:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Dec 2012 14:54:45 -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.215.54 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.215.54 mail-la0-f54.google.com Received: from [209.85.215.54] ([209.85.215.54:34089] helo=mail-la0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0B/4A-20281-33874D05 for ; Fri, 21 Dec 2012 09:54:44 -0500 Received: by mail-la0-f54.google.com with SMTP id j13so5230932lah.27 for ; Fri, 21 Dec 2012 06:54:40 -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=69xci1wFJnwP9odhF+0n0JaJo3OfWneenjzwfkmPenk=; b=E/aQmDCVqtEr29kqk8OSIvpvBUpFLd0QZcsvlrcsR7uSkAMAkJAnrOVYE2BoCHD7/D 8euzXii+NUJvH8Ok4K87HaRW1qdLeJLvcXOrejIvqXUXUktVHNTRSKBhoZQSQqDKC0Yq LL820WBoIbNrfa7RVMoMhz6XdVwV3cMr3I7LihUUuDkh5HUbzNgLcE2wMfvnI4wBETQv JYedIRKiRhjXACer/PAG1M6um/W9D6K8dSHsI0J+6JFQ2j6DwhIkd/x0CtZgwfDFH/Co v3sBBr5xWQErlysFvnHOqIdtB5taDEWOarBeWvHXss4BPP0Pk4GL74gCvMzvl1C0GICP gHIg== MIME-Version: 1.0 Received: by 10.152.162.1 with SMTP id xw1mr12694490lab.3.1356101680420; Fri, 21 Dec 2012 06:54:40 -0800 (PST) Received: by 10.112.4.168 with HTTP; Fri, 21 Dec 2012 06:54:40 -0800 (PST) In-Reply-To: References: <50CF969D.8090707@sugarcrm.com> Date: Fri, 21 Dec 2012 15:54:40 +0100 Message-ID: To: Stas Malyshev Cc: PHP internals Content-Type: multipart/alternative; boundary=f46d042ef4a5f0d55404d15e066a Subject: Re: [PHP-DEV] Adding Generator::throw() From: nikita.ppv@gmail.com (Nikita Popov) --f46d042ef4a5f0d55404d15e066a Content-Type: text/plain; charset=ISO-8859-1 On Mon, Dec 17, 2012 at 11:45 PM, Nikita Popov wrote: > 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 > If there are no further objections I'll commit this tomorrow. Nikita --f46d042ef4a5f0d55404d15e066a--