Re: Asyncio -- delayed calculation

2016-12-02 Thread Chris Angelico
On Sat, Dec 3, 2016 at 1:26 AM, Frank Millman wrote: > Then Twisted made a strong case for an asynchronous approach. One of their > claims (which I have no reason to doubt) was that, because each user > 'session' spends most of its time waiting for something - keyboard input,

Re: Asyncio -- delayed calculation

2016-12-02 Thread Marko Rauhamaa
"Frank Millman" : > Then Twisted made a strong case for an asynchronous approach. One of > their claims (which I have no reason to doubt) was that, because each > user 'session' spends most of its time waiting for something - > keyboard input, reply from database, etc - their

Re: Asyncio -- delayed calculation

2016-12-02 Thread Frank Millman
"Steve D'Aprano" wrote in message news:58417e2d$0$1612$c3e8da3$54964...@news.astraweb.com... My first impressions on this is that we have a couple of good models for preemptive parallelism, threads and processes, both of which can do everything that concurrency can do, and more, and both of

Re: Asyncio -- delayed calculation

2016-12-02 Thread Marko Rauhamaa
Steve D'Aprano : > py> await x > File "", line 1 > await x > ^ > SyntaxError: invalid syntax "await" is only allowed inside a coroutine. > So why do we need asyncio? What is it actually good for? Asyncio is a form of cooperative multitasking. It

Re: Asyncio -- delayed calculation

2016-12-02 Thread Steve D'Aprano
On Thu, 1 Dec 2016 06:53 pm, Christian Gollwitzer wrote: > well that works - but I think it it is possible to explain it, without > actually understanding what it does behind the scences: > > x = foo() > # schedule foo for execution, i.e. put it on a TODO list > > await x > # run the TODO list

Re: Asyncio -- delayed calculation

2016-12-01 Thread Ian Kelly
On Thu, Dec 1, 2016 at 12:53 AM, Christian Gollwitzer wrote: > well that works - but I think it it is possible to explain it, without > actually understanding what it does behind the scences: > > x = foo() > # schedule foo for execution, i.e. put it on a TODO list This implies

Re: Asyncio -- delayed calculation

2016-11-30 Thread Christian Gollwitzer
Am 30.11.16 um 22:07 schrieb Gregory Ewing: Chris Angelico wrote: That's because you're not actually running anything concurrently. Yes, I know what happens and why. My point is that for someone who *doesn't* know, simplistic attempts to explain what "await" means can be very misleading.

Re: Asyncio -- delayed calculation

2016-11-30 Thread Marko Rauhamaa
Gregory Ewing : > My point is that for someone who *doesn't* know, simplistic attempts > to explain what "await" means can be very misleading. > > There doesn't seem to be any accurate way of summarising it in a few > words. The best we can do seems to be to just say

Re: Asyncio -- delayed calculation

2016-11-30 Thread Marko Rauhamaa
Terry Reedy : > On 11/30/2016 7:53 AM, Chris Angelico wrote: > >> I also think that everyone should spend some time writing >> multithreaded code before switching to asyncio. It'll give you a >> better appreciation for what's going on. > > I so disagree with this. I have written

Re: Asyncio -- delayed calculation

2016-11-30 Thread Gregory Ewing
Chris Angelico wrote: That's because you're not actually running anything concurrently. Yes, I know what happens and why. My point is that for someone who *doesn't* know, simplistic attempts to explain what "await" means can be very misleading. There doesn't seem to be any accurate way of

Re: Asyncio -- delayed calculation

2016-11-30 Thread Chris Kaynor
On Wed, Nov 30, 2016 at 10:58 AM, Chris Angelico wrote: > On Thu, Dec 1, 2016 at 5:54 AM, Terry Reedy wrote: >> On 11/30/2016 7:53 AM, Chris Angelico wrote: >> >>> I also think that everyone should spend some time writing >>> multithreaded code before

Re: Asyncio -- delayed calculation

2016-11-30 Thread Chris Angelico
On Thu, Dec 1, 2016 at 5:54 AM, Terry Reedy wrote: > On 11/30/2016 7:53 AM, Chris Angelico wrote: > >> I also think that everyone should spend some time writing >> multithreaded code before switching to asyncio. It'll give you a >> better appreciation for what's going on. > > >

Re: Asyncio -- delayed calculation

2016-11-30 Thread Terry Reedy
On 11/30/2016 7:53 AM, Chris Angelico wrote: I also think that everyone should spend some time writing multithreaded code before switching to asyncio. It'll give you a better appreciation for what's going on. I so disagree with this. I have written almost no thread code but have

Re: Asyncio -- delayed calculation

2016-11-30 Thread Chris Angelico
On Wed, Nov 30, 2016 at 11:40 PM, Gregory Ewing wrote: > Chris Angelico wrote: >> >> From the point of view of >> the rest of Python, no. It's a sign saying "Okay, Python, you can >> alt-tab away from me now". > > > The problem with that statement is it implies that

Re: Asyncio -- delayed calculation

2016-11-30 Thread Gregory Ewing
Chris Angelico wrote: From the point of view of the rest of Python, no. It's a sign saying "Okay, Python, you can alt-tab away from me now". The problem with that statement is it implies that if you omit the "await", then the thing you're calling will run uninterruptibly. Whereas what actually

Re: Asyncio -- delayed calculation

2016-11-30 Thread Gregory Ewing
On Tuesday 29 November 2016 14:21, Chris Angelico wrote: "await" means "don't continue this function until that's done". It blocks the function until a non-blocking operation is done. That explanation gives the impression that it's some kind of "join" operation on parallel tasks, i.e. if you

Re: Asyncio -- delayed calculation

2016-11-29 Thread Steve D'Aprano
On Wed, 30 Nov 2016 05:41 am, Ian Kelly wrote: > You mean how do you create something that can be awaited that doesn't > await something else in turn? With a Future. > > import asyncio > > class Awaitable(asyncio.Future): > def wake_up_later(self): > asyncio.get_event_loop().call_later(3,

Re: Asyncio -- delayed calculation

2016-11-29 Thread Ian Kelly
On Mon, Nov 28, 2016 at 10:42 PM, Chris Angelico wrote: > On Tue, Nov 29, 2016 at 4:13 PM, Paul Rubin wrote: >> >> I haven't gotten my head around Python asyncio and have been wanting >> to read this: >> >>

Re: Asyncio -- delayed calculation

2016-11-28 Thread Marko Rauhamaa
Gregory Ewing : > All the terminology around async/await is inherently confusing and > counterintuitive, IMO. I'm disappointed that we've ended up here. I think the conceptual mess can be clarified over time. Coroutines are essentially threads. Why Python needs two

Re: Asyncio -- delayed calculation

2016-11-28 Thread Chris Angelico
On Tue, Nov 29, 2016 at 4:32 PM, Steven D'Aprano wrote: > On Tuesday 29 November 2016 14:21, Chris Angelico wrote: > >> On Tue, Nov 29, 2016 at 1:23 PM, Steve D'Aprano >> wrote: >>> This is confusing: why is this awaiting

Re: Asyncio -- delayed calculation

2016-11-28 Thread Chris Angelico
On Tue, Nov 29, 2016 at 4:13 PM, Paul Rubin wrote: > > I haven't gotten my head around Python asyncio and have been wanting > to read this: > >http://lucumr.pocoo.org/2016/10/30/i-dont-understand-asyncio/ It's talking a lot about how we got here, which isn't all

Re: Asyncio -- delayed calculation

2016-11-28 Thread Steven D'Aprano
On Tuesday 29 November 2016 14:21, Chris Angelico wrote: > On Tue, Nov 29, 2016 at 1:23 PM, Steve D'Aprano > wrote: >> This is confusing: why is this awaiting something inside an async function? >> Doesn't that mean that the await asyncio.gather(...) call is turned >>

Re: Asyncio -- delayed calculation

2016-11-28 Thread Paul Rubin
Chris Angelico writes: > Asynchronous I/O is something to get your head around I'd much > rather work with generator-based async functions... I haven't gotten my head around Python asyncio and have been wanting to read this:

Re: Asyncio -- delayed calculation

2016-11-28 Thread Nathan Ernst
To be fair, in other languages, such as C# or C++ with similar mechanisms, if you don't ask for the result from an async or future task, there's no guarantee the async task will be executed at all unless (or until) you ask for the result. C++'s futures even give an explicit flag indicating you

Re: Asyncio -- delayed calculation

2016-11-28 Thread Chris Angelico
On Tue, Nov 29, 2016 at 3:16 PM, Gregory Ewing wrote: > Chris Angelico wrote: >> >> "await" means "don't continue this function until that's done". It >> blocks the function until a non-blocking operation is done. > > > However, *not* using 'await' doesn't mean the

Re: Asyncio -- delayed calculation

2016-11-28 Thread Gregory Ewing
Chris Angelico wrote: "await" means "don't continue this function until that's done". It blocks the function until a non-blocking operation is done. However, *not* using 'await' doesn't mean the operation will be done without blocking. Rather, it won't be done at all (and is usually an error,

Re: Asyncio -- delayed calculation

2016-11-28 Thread Zachary Ware
On Mon, Nov 28, 2016 at 6:48 AM, Steve D'Aprano wrote: > What am I doing wrong? Give yourself a bit more to debug with, since you're going to want to do something with the result your expensive calculation anyway: import asyncio class Counter: def __init__(self,

Re: Asyncio -- delayed calculation

2016-11-28 Thread Chris Angelico
On Tue, Nov 29, 2016 at 1:23 PM, Steve D'Aprano wrote: > This is confusing: why is this awaiting something inside an async function? > Doesn't that mean that the await asyncio.gather(...) call is turned > blocking? "await" means "don't continue this function until

Re: Asyncio -- delayed calculation

2016-11-28 Thread Zentrader
Take a look at Doug Hellmann's example using multiprocessing at https://pymotw.com/2/multiprocessing/basics.html You should be able to substitute the count down example directly into the first example. -- https://mail.python.org/mailman/listinfo/python-list

Re: Asyncio -- delayed calculation

2016-11-28 Thread Steve D'Aprano
On Tue, 29 Nov 2016 12:03 am, Chris Angelico wrote: > On Mon, Nov 28, 2016 at 11:48 PM, Steve D'Aprano > wrote: >> When I try running that, I get no output. No error, no exception, the >> run_until_complete simply returns instantly. > > When I do, I get this warning:

Re: Asyncio -- delayed calculation

2016-11-28 Thread Chris Angelico
On Tue, Nov 29, 2016 at 11:20 AM, Steve D'Aprano wrote: > On Tue, 29 Nov 2016 02:53 am, Ian Kelly wrote: > >> In order for the coroutines to actually do anything, you need to >> schedule them in some way with the event loop. That could take the >> form of awaiting them

Re: Asyncio -- delayed calculation

2016-11-28 Thread Steve D'Aprano
On Tue, 29 Nov 2016 02:53 am, Ian Kelly wrote: > In order for the coroutines to actually do anything, you need to > schedule them in some way with the event loop. That could take the > form of awaiting them from some other coroutine, or passing them > directly to loop.run_until_complete or

Re: Asyncio -- delayed calculation

2016-11-28 Thread Ian Kelly
On Mon, Nov 28, 2016 at 5:48 AM, Steve D'Aprano wrote: > Let's pretend that the computation can be performed asynchronously, so that > I can have all five Counter objects counting down in parallel. I have this: > > > import asyncio > > class Counter: > def

Re: Asyncio -- delayed calculation

2016-11-28 Thread Chris Angelico
On Mon, Nov 28, 2016 at 11:48 PM, Steve D'Aprano wrote: > When I try running that, I get no output. No error, no exception, the > run_until_complete simply returns instantly. When I do, I get this warning: asynctest.py:17: RuntimeWarning: coroutine

Asyncio -- delayed calculation

2016-11-28 Thread Steve D'Aprano
I'm a complete and utter newbie when it comes to asynchronous programming, so I may have the entire concept backwards here. But treat this as a learning exercise rather than something I'd really do. Suppose I have a bunch of calculations to do: count down from 10. So I have a bunch of objects: