How asyncio works? and event loop vs exceptions

2016-07-22 Thread Marco S. via Python-list
I'm developing a web app based on aiohttp, and I find the event loop
concept very interesting. I never programmed with it before, but I
know that node.js and GUIs are based on it.

What I can't understand is how asyncio make it possible to run
multiple tasks concurrently, since it's single threaded (if you don't
use loop.run_in_executor()). I just tried to imagine that it should
act as a cpu scheduler. Is this true? If so, how and when asyncio
decide to switch to another task?

Furthermore I have a question about exceptions in asyncio. If I
understand well how it works, tasks exceptions can be caught only if
you wait for task completion, with yield from, await or
loop.run_until_complete(future). But in this case the coroutine blocks
the execution of the program until it returns. On the contrary you can
execute the coroutine inside an asyncio task and it will be
non-blocking, but in this case exceptions can't be caught in a try

statement.

Is this correct? If so, asyncio programming style can't be a little
divergent from what was the philosophy of Python until now?
-- 
https://mail.python.org/mailman/listinfo/python-list


Suggestion: make sequence and map interfaces more similar

2016-03-23 Thread Marco S. via Python-list
I noticed that the sequence types does not have these methods that the map
types  has: get(), items(), keys(), values().

It could seem useless to have them for sequences, but I think it will ease
the creation of functions and methods that allow you to input a generic
iterable as parameter, but needs to use one of these methods in case the
parameter is a map.

In one word, it will facilitate duck typing.

For the same reason, I would suggest the introduction of a new map type,
vdict, a dict that by default iterates over values instead over keys. So a
vdict object "d" wiil have iter(d) == iter(d.values()), and should also
have a count() method, like sequence types.

Indeed sequences are, in my humble opinion, a specialized case of maps,
when keys are numeric only, are always contiguous without gaps and start
from 0. This way we will have a simpler way to let people to use sequences
or maps indifferently, and let the code untouched.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Suggestion: make sequence and map interfaces more similar

2016-03-27 Thread Marco S. via Python-list
Steven D'Aprano wrote:

> The point you might have missed is that treating lists as if they were
> mappings violates at least one critical property of mappings: that the
> relationship between keys and values are stable.


This is true for immutable maps, but for mutable ones, you can simply do

map[key] = new_value

You can easily create a new class that extend dict and add to it a
method that emulated the insert method of lists. It will definitively
not break any contract, if your contract is "sequences are maps with
ordered integer keys that starts from zero and have no gaps".

Mark Lawrence wrote:

> I cannot see this happening unless you provide a patch on the bug
> tracker.  However I suspect you can get the same thing by subclassing
> dict.  Why don't you try it and let us know how you get on?

The problem with a vdict is that I should also create by zero a new
interface (ABC), since MutableMapping have an incompatible contract.
And to be much more useful I should code it inc C, and my C skills are
not so magnificent. Furthermore I would try to create a new sequence
interface that inherit from the vdict interface. More briefly, I need
much free time. But I suppose I can start with something quick and
dirty.
-- 
https://mail.python.org/mailman/listinfo/python-list