Yet another asyncio tutorial that shows you to run a few sleep tasks concurrently. Can we finally get one that shows how to do <i>real</i> stuff such like socket programming, wrapping non-async-compatible libraries and separating cpu-intensive blocking tasks to awaitable threads?
Python is my language of first choice, but I must say that I am not that thrilled how this multithreading ended up. There are many tutorials about the topic promising to explain how it works, usually in the form of "simple introduction". But when one tries to implement something production-ready, with correct error handling etc., things starts to complicate pretty quickly; at least that was my experience. I don't want to accuse anyone specifically, but most of the tutorials I saw seems to portrait it in a way, that it looks easier than it actually is.<p>Ultimately, my company decided, that instead of fighting with asyncio, certain projects will switch to Go.
This is essentially how modern JavaScript works, in particular with the addition of async/await syntax [1] (which was originally from C#, I think), but it's been possible with libraries like task.js, co, and Bluebird [2] since generator functions were available (either natively or via transpiling).<p>The main difference is in JavaScript the event loop is automatic and hidden, and asynchronous IO is the default, so it's a bit harder to shoot yourself in the foot.<p>1. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...</a><p>2. <a href="https://github.com/mozilla/task.js" rel="nofollow">https://github.com/mozilla/task.js</a> <a href="https://github.com/tj/co" rel="nofollow">https://github.com/tj/co</a> <a href="http://bluebirdjs.com/docs/api/promise.coroutine.html" rel="nofollow">http://bluebirdjs.com/docs/api/promise.coroutine.html</a>
I’m still bummed that Python took this direction. Maybe introducing new keywords into the language for event loop concurrency was Python’s way of satisfying “explicit is better than implicit” but i can’s shake the feeling that callback passing and generator coroutines are a fad that is complex enough to occupy the imagination of a generation of programmers while offering little benefit compared to green threads.
quoting the article:<p>> Concurrency is like having two threads running on a single core CPU.<p>> Parallelism is like having two threads running simultaneously on different cores<p>> It is important to note that parallelism implies concurrency but not the other way round.<p>Aurgh! I don't think this attempted definition-by-simile is helpful, or even somewhat correct.<p>I much prefer yosefk's way of framing things:<p>> > concurrent (noun): Archaic. a rival or competitor.<p>> > Two lines that do not intersect are called parallel lines.<p>...<p>> Computation vs event handling<p>> With event handling systems such as vending machines, telephony, web servers and banks, concurrency is inherent to the problem – you must resolve inevitable conflicts between unpredictable requests. Parallelism is a part of the solution - it speeds things up, but the root of the problem is concurrency.<p>> With computational systems such as gift boxes, graphics, computer vision and scientific computing, concurrency is not a part of the problem – you compute an output from inputs known in advance, without any external events. Parallelism is where the problems start – it speeds things up, but it can introduce bugs.<p>...<p>> concurrency is dealing with inevitable timing-related conflicts, parallelism is avoiding unnecessary conflicts<p>yosefk's whole essay about this is great: <a href="https://yosefk.com/blog/parallelism-and-concurrency-need-different-tools.html" rel="nofollow">https://yosefk.com/blog/parallelism-and-concurrency-need-dif...</a>
I really liked this article. It's by far the most concise explanation of asyncio in python that I've come across. Also, great use of little "quoted" statements throughout that encourage you to stop and really understand what was said before moving on (these probably have a special name).<p>Bravo!
A few years ago I wrote an BitTorrent client in Python 3.5 to get to know asyncio better.<p>Maybe those blogposts are still of use to somebody:<p>- <a href="http://markuseliasson.se/article/introduction-to-asyncio/" rel="nofollow">http://markuseliasson.se/article/introduction-to-asyncio/</a><p>- <a href="http://markuseliasson.se/article/bittorrent-in-python/" rel="nofollow">http://markuseliasson.se/article/bittorrent-in-python/</a>
Here is a comparison of `asyncio` (Python), `async` (Ruby) and Go: <a href="https://github.com/socketry/async-await/tree/master/examples/port_scanner" rel="nofollow">https://github.com/socketry/async-await/tree/master/examples...</a><p>I wrote a similar article but for Ruby: <a href="https://www.codeotaku.com/journal/2018-06/asynchronous-ruby/index" rel="nofollow">https://www.codeotaku.com/journal/2018-06/asynchronous-ruby/...</a><p>Yes, it's a good model for many use cases.<p>One thing I wondered about Ruby, is it really necessary to have the `await` keyword?
It seems odd that Python ended up with asyncio when they had a clear and successful model they could’ve adopted from Stackless. It would have been more difficult to implement, but it would allow for the same benefits without requiring an asynchronous programming model, which would have reduced the total amount of effort involved in getting to having good concurrency in Python.
Wasn't there a language where every call was async? Instead of async ... A/returing Future[A] it did/would return A from method calls.<p>If it didn't exist, one can imagine one.<p>A.x = 3 would be wrapped in A.map(_.x = 3) etc. So you write code that would be executed when you finally await a value. No more red/blue world. Would probably need coroutines instead of threads for executing.
Someone send this article to Armin Ronacher (creator of Flask, Jinja, etc.) so he can understand asyncio since he wrote a much longer and more detailed article explaining why he doesn't understand asyncio (previous discussion at <a href="https://news.ycombinator.com/item?id=12829759" rel="nofollow">https://news.ycombinator.com/item?id=12829759</a>).
My first endeavor with asyncio felt worse than a beating with a wet rubber hose. But it was a character building experience to really get up close and personal with the asynchronous model and I can definitely see its advantages over imperative, as well as it's always good to have another tool in the box.
Thinly veiled advertisement for a "Intelligent Infrastructure Analytics - a Machine Learning driven approach for DevOps & SREs of modern age" company. Seems like hackernoon just published a native advertisement? Not surprisingly shady though, considering the "buy crypto with credit card" link in the top bar.