| Store | Cart

Re: [Python-Dev] PEP 492: What is the real goal?

From: Yury Selivanov <ysel...@gmail.com>
Wed, 29 Apr 2015 14:42:34 -0400
Hi Paul,

On 2015-04-29 2:26 PM, Paul Moore wrote:
> On 29 April 2015 at 18:43, Jim J. Jewett <jimj...@gmail.com> wrote:>>> Rationale and Goals>>> ===================>>>>>> Current Python supports implementing coroutines via generators (PEP>>> 342), further enhanced by the ``yield from`` syntax introduced in PEP>>> 380. This approach has a number of shortcomings:>>>>>> * it is easy to confuse coroutines with regular generators, since they>>>    share the same syntax; async libraries often attempt to alleviate>>>    this by using decorators (e.g. ``@asyncio.coroutine`` [1]_);>> So?  PEP 492 never says what coroutines *are* in a way that explains>> why it matters that they are different from generators.> I agree. While I don't use coroutines/asyncio, and I may never do so,> I will say that I find Python's approach very difficult to understand.>> I'd hope that the point of PEP 492, by making await/async first class> language constructs, would be to make async programming more> accessible in Python. Whether that will actually be the case isn't> particularly clear to me. And whether "async programming" and> "coroutines" are the same thing, I'm even less sure of. I haven't> really followed the discussions here, because they seem to be about> details that are completely confusing to me.

It will make it more accessible in Python.  asyncio is getting
a lot of traction, and with this PEP accepted I can see it
only becoming easier to work with it (or any other async
frameworks that start using the new syntax/protocols).

>> In principle, I support the PEP, on the basis that working towards> better coroutine/async support in Python seems worthwhile to me. But> until the whole area is made more accessible to the average> programmer, I doubt any of this will be more than a niche area in> Python.>> For example, the PEP says:>> """> New Coroutine Declaration Syntax>> The following new syntax is used to declare a coroutine:>> async def read_data(db):>      pass> """>> Looking at the Wikipedia article on coroutines, I see an example of> how a producer/consumer process might be written with coroutines:>> var q := new queue>> coroutine produce>      loop>          while q is not full>              create some new items>              add the items to q>          yield to consume>> coroutine consume>      loop>          while q is not empty>              remove some items from q>              use the items>          yield to produce>> (To start everything off, you'd just run "produce").>> I can't even see how to relate that to PEP 429 syntax. I'm not allowed> to use "yield", so should I use "await consume" in produce (and vice> versa)? I'd actually expect to just write 2 generators in Python, and> use .send() somehow (it's clunky and I can never remember how to write> the calls, but that's OK, it just means that coroutines don't have> first-class syntax support in Python). This is totally unrelated to> asyncio, which is the core use case for all of Python's async support.> But it's what I think of when I see the word "coroutine" (and> Wikipedia agrees).

That Wikipedia page is very generic, and the pseudo-code
that it uses does indeed look confusing.

Here's how it might look like (this is the same pseudo-code
but tailored for PEP 492, not a real something)

   q = asyncio.Queue(maxsize=100)

   async def produce():
       # you might want to wrap it all in 'while True'

       while not q.full():
           item = create_item()
           await q.put(item)

   async def consume():
       while not q.empty():
           item = await q.get()
           process_item(item)


Thanks!
Yury
_______________________________________________
Python-Dev mailing list
Pyth...@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/python-dev-ml%40activestate.com

Recent Messages in this Thread
Jim J. Jewett Apr 29, 2015 05:43 pm
Paul Moore Apr 29, 2015 06:26 pm
Yury Selivanov Apr 29, 2015 06:42 pm
Paul Moore Apr 29, 2015 07:19 pm
Yury Selivanov Apr 29, 2015 07:42 pm
Skip Montanaro Apr 29, 2015 08:14 pm
Nathaniel Smith Apr 29, 2015 08:19 pm
Guido van Rossum Apr 29, 2015 08:30 pm
Greg Ewing Apr 30, 2015 06:53 am
Paul Sokolovsky Apr 30, 2015 07:58 am
Paul Moore Apr 29, 2015 10:02 pm
Greg Ewing Apr 30, 2015 06:35 am
Arnaud Delobelle Apr 30, 2015 08:50 am
Oscar Benjamin May 05, 2015 04:01 pm
Yury Selivanov May 05, 2015 04:48 pm
Guido van Rossum May 05, 2015 07:24 pm
Oscar Benjamin May 06, 2015 09:20 am
Paul Sokolovsky Apr 29, 2015 09:06 pm
Greg Ewing Apr 30, 2015 12:08 pm
Paul Moore Apr 30, 2015 06:52 pm
Greg Ewing Apr 30, 2015 05:39 am
Paul Moore Apr 30, 2015 08:17 am
Jim J. Jewett Apr 30, 2015 05:24 pm
Guido van Rossum Apr 30, 2015 05:55 pm
Guido van Rossum Apr 28, 2015 09:49 pm
Yury Selivanov Apr 28, 2015 11:26 pm
Ethan Furman Apr 28, 2015 11:55 pm
Guido van Rossum Apr 29, 2015 12:04 am
Ethan Furman Apr 29, 2015 05:18 am
Greg Ewing Apr 29, 2015 09:12 am
Yury Selivanov Apr 29, 2015 05:00 am
Greg Ewing Apr 29, 2015 09:13 am
Yury Selivanov Apr 29, 2015 02:01 pm
Greg Ewing Apr 29, 2015 10:46 pm
Yury Selivanov Apr 29, 2015 10:58 pm
Greg Ewing Apr 29, 2015 09:12 am
Yury Selivanov Apr 29, 2015 02:16 pm
Greg Apr 29, 2015 03:59 am
Yury Selivanov Apr 29, 2015 04:10 am
Greg Ewing Apr 29, 2015 09:13 am
Yury Selivanov Apr 29, 2015 02:18 pm
Greg Ewing Apr 29, 2015 09:12 am
Yury Selivanov Apr 29, 2015 02:29 pm
Messages in this thread