Is this pythonic?

Frank Millman frank at chagford.com
Thu Nov 24 02:33:14 EST 2016


"Steven D'Aprano"  wrote in message 
news:58368358$0$1513$c3e8da3$5496439d at news.astraweb.com...

> On Thursday 24 November 2016 15:55, Frank Millman wrote:
>
> > "Steve D'Aprano"  wrote in message
> > news:583653bb$0$1603$c3e8da3$5496439d at news.astraweb.com...
> >
> >> Even if the computation of the memoised value is done asynchronously, 
> >> you
> >> can easily split the computation off to a separate method (as you 
> >> already
> >> talked about doing!) and make getval() block until it returns.
> >
> > Surely that defeats the whole purpose of asyncio. Anything that blocks 
> > holds
> > up the entire process. I strenuously try to avoid blocking in any shape 
> > or
> > form.
>
> Perhaps I'm not understanding your task correctly, but surely you have to 
> wait
> for the computation to occur at some point? Even if that's just you 
> hitting
> Refresh waiting for the value of the column to eventually show up.
>
> I'm a newbie to asyncio, but if I were doing this using threads, I'd have
> getval() set the self._cached_value to "pending..." (say), start the
> computation thread running, and then return. The computation thread will
> eventually write the true value to _cached_value, and in the meantime the
> getval() method (and hence __str__ will happily use the "pending..." 
> value. The
> only tricky part is to make sure you only start the thread once.
>

I am not really qualified to answer this - I *use* asyncio, but I don’t 
really understand what goes on under the covers.

With that caveat, here goes.

To me, the beauty of asyncio (or I suppose async in general) is that I don't 
have to worry about any of what you describe above.

I just have to 'await' whatever I am waiting for. There could be a long 
chain of function calls (which I suppose I should call coroutine calls) but 
at some point one of them is actually going to wait for some I/O, and yield 
control back to the event loop.

At that point, the entire chain is suspended, pending return of the value. 
Once received, control passes back down the chain to the originating 
coroutine, which can then carry on exactly where it left off.

Frank





More information about the Python-list mailing list