Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88593 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 76718 invoked from network); 1 Oct 2015 06:59:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Oct 2015 06:59:40 -0000 X-Host-Fingerprint: 68.118.157.39 68-118-157-39.dhcp.mdsn.wi.charter.com Received: from [68.118.157.39] ([68.118.157.39:20665] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F6/11-04700-BD9DC065 for ; Thu, 01 Oct 2015 02:59:39 -0400 Message-ID: To: internals@lists.php.net References: <56095A0C.4070306@cubiclesoft.com> <1443529244.15520.67.camel@kuechenschabe> <560AACB6.9080909@cubiclesoft.com> Date: Thu, 1 Oct 2015 01:59:36 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <560AACB6.9080909@cubiclesoft.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 68.118.157.39 Subject: Re: [PHP-DEV] async/await - is future reserved words in PHP 7.x? From: me@stephencoakley.com (Stephen Coakley) On 09/29/2015 10:22 AM, Thomas Hruska wrote: > On 9/29/2015 6:52 AM, Joe Watkins wrote: >> We shouldn't reserve words on a whim ... >> >> async/await doesn't solve any problems for multithreaded programming, at >> all ... it solves problems for asynchronous programming, a different >> concept ... let's not confuse the two ... Agreed, the concepts are quite different, thought they can be related in a way. More on that in a minute... > Actually, it does. Asynchronous and multithreaded programming are > attempts at solving the exact same problem: Simultaneously handling > multiple data streams in whatever form that takes whether that be > network, hard drive, random device XYZ, RAM, or even CPU. The former > says, "Let's wait until we can read or write and continue when we have > data." The latter says, "Let's read and write but let the OS handle > context switching to give the appearance of simultaneously handling > multiple data streams." Actually, they *don't* attempt to solve the same problem (though some problems can be solved with either). Concurrent programming (threads, forks, or anything else that uses multiple execution contexts -- multiple program counters) has the purpose of enabling you to do two or more things at once (as much as possible on the current CPU). If you need to do two big ass math calculations, you can use multithreading to do both at once. (I'll ignore instruction pipelining restrictions and the context switching overhead curve for simplicity.) In this example, asynchronous programming does nothing for us. If you need to handle 10000 socket connections, threading isn't practical with a thread-per-connection model. Here, async becomes useful because the bottleneck is I/O, not CPU operations. Async programming allows you to do other things instead of blocking the thread when doing an I/O operation (... or a few other operation types). So then, in summary, threads & multiprocessing enables you to do more *work* simultaneously, while async **maximizes the usefulness of each thread you have**. So using both is actually an incredibly good thing, but they aren't the same nor accomplish the same thing. I hope what I said made sense. Now, on to async / await. :) > Async/await is both the best of and THE solution to both worlds. It > ... Yes. But not for the reason that I think you're thinking. Combining concurrency with asynchronicity is the best of both worlds, and using threads themselves in a non-blocking way with async / await (which are strictly an async thing) is pretty powerful. Here's an example (in .NET): static async void SlowAsync() { // Create a task which will be run in the threadpool Task task = new Task(SlowAssOperation); // Asynchronously wait (using the .NET event loop) for the thread to complete await task; } static void SlowAssOperation() { // Do something synchronous Thread.Sleep(5000); } The fact that we can await something being run in a thread isn't a property of threads, but a property of the .NET async event loop which can listen for thread events without blocking. Hope that clears some things up. Sorry for being long winded. :) -- Stephen