125 points · by tbarbugli · 9y ago · 37 comments
Thanks :)
result = [i async for i in aiter() if i % 2]
won't speed anything up or put multiple CPUs to work on it.Python's new "async" features are useful mostly for servers running many network connections simultaneously. Too many for one thread per connection. That use case used to be handled by "Twisted Python", but now there's language support. That's useful. But a list comprehension doesn't make sense for that use case. The PEP lacks any useful example.
The motivation seems to be to make async comprehensions have the same capabilities as regular comprehensions, even though that's not particularly useful.
It would be really nice if somehow these kinds of tasks could be tied into the multiprocessing package, so that async could handle processor intensive applications well.
I long for a dynamic but strictly typed language with rich standard lib that gets this right. Nim?
1. read csv 2. create data structure 3. calculate statistics for each column (max values, unique values, etc.) 4. combine those statistics with original columns to generate new columns 5. write new columns to new csv
Processing each type of statistics in its own thread, combined with promises, allowed the processors to independently work on the same data structure. Because of the need to handle non-numeric values in numeric columns, e.g. convert nulls to zeroes, matrix manipulations would not have been appropriate.
I just wanted to point out a type of situation from my experience that goes against your generalization, to give a more complete picture.
With my point about the need to plan ahead I specifically meant python by the way, and I think all runtimes that can't make use of concurrent multithreading have this in common. As soon as you can do multithreading with shared memory you become a lot more flexible in parallelising later on, and you can adjust your code style accordingly to accommodate this without much overhead. That's why the GIL IMO is so unfortunate, it's a real shame that Python 3 wasn't able to get rid of this when it started with a clean slate.
BTW, as far as I recall Nim is statically typed, please correct me if I am wrong.
Python is still fundamentally a n=1 language, where only one thread can run the interpreter at any time (GIL), like many scripting languages. Thus any gains in performance from using multiple threads has to stem from using external resources (syscalls, calls into C code) -- running pure Python code in multiple threads won't increase performance (but still provide concurrency).
Most libraries that do heavy lifting in native code and so on are written with this in mind, and allow thus ample performance gains, should these operations actually limit performance.
Of course, nothing keeps one from using entirely different means to concurrency, eg. message passing, like the ZeroMQ approach, RPC, shared memory and so on are all possible with Python.
So I'd say it's quite manageable, and that the GIL isn't really a significant limitation for most applications. However, it also means that in many cases process-level parallelism is preferable, eg. in the context of web applications this is the usual approach (and it doesn't have to cost a ton of memory - see uwsgi/pre-forking appservers), besides async.
Another important async improvement in 3.6 are asynchronous iterators:
https://www.python.org/dev/peps/pep-0525/
(yielding from a coroutine)
responses = [await r() for r in requests]
... but, what's the 'practical' application of `async` in comprehensions? I can't think of one that shouldn't call an independent `async` function for readability.e.g.: Replace:
require 'open-uri'
(1..100).map { |i| open("http://httpbin.org/get?a=#{i}").read }
To: require 'open-uri'
require 'pmap'
(1..100).pmap { |i| open("http://httpbin.org/get?a=#{i}").read }
From 17.55s to 0.72s. import requests
import multiprocessing as mp
pool = mp.Pool()
urls = ["http://httpbin.org/get?a=#{}".format(i) for i in range(100)]
pool.map(requests.get, urls)hint: I'm implying familiarity over elegance.
list(map(lambda x: x**2, [1, 2, 3, 4]))
To: list(pmap(lambda x: x**2, [1, 2, 3, 4]))This comment may relate better to your point: I would personally prefer "afor" over "async for", FWIW. One thing I don't feel "async for" is being more explicit over being more verbose. The symmetry with "await foo" is broken though.
Downvotes? Would this have been upvoted?:
"Python 3.6 is really cool and looks fairly compact, especially when you're used to nesting callbacks, using promises, or fibers in Node.js. That said, arrow functions provide some of that compactness in ES6."
It's interesting how a certain verbal style resonates better or worse in certain communities, even if the content is held approximately fixed:
"Python 3.6 is really cool because of comprehensions."
"Python 3.6 is lit af bruh cuz dem list comps."
"dis lang is dope cuz ONE liners tho."
"python is fucking [i for its in me]"
For example, if I tried to teach a bunch of inner city kids about Python sounding like the average HackerNews user, they would never learn anything! For those of you that teach kids, you'll probably understand, everyone else will probably take offense to this, don't say I didn't warn you.
Communities like this (HackerNews) lack diversity -- we're all the same flavor of nerd, and that's boring as fuck, isn't it? We tend to agree with each other, and are diametrically opposed to people who try to burst our bubble. Clearly we're right about everything because we have been validated by our fancy BS's in honors physics, mathematics, chemistry, biology, and other sciences (oh, and engineering, if you're second-rate (shh! some people don't like mean jokes)).
There was a post on here about democracy after the Trump victory (or right before?). And the majority consensus of the nerds on this site was that government should not have unlimited power, and it should make itself easy to change, or replace, if needed. I.e., there should be no runaway governments, which we're stuck with. People should have the power to change things. For the most part, you guys agree with that, which I find incredibly ironic.
So I think we are OK.
HN is indeed a bubble, but it's a fantastic one that would lose a lot of value if the liquid it came from was diluted.
Sure, they might both be equally deep when you get to know them (yeah, right), but appearances do matter and/or can get in one's nerves.
I wouldn't try to do that myself, precisely because they're illiterate. They have much bigger problems than the accessibility of learning a programming language. They need to be functional members of society, and one important part of that is communication.