|Using Asyncio in Python|
Author: Caleb Hattingh
Python is a bit of a mess when it comes to concurrent programming. Yes, you can do threads, but the GIL stops any attempt you might make at truly parallel programming. In Python threading is asynchronous code controlled by the operating system. Asyncio is asynchronous code controlled by Python and as such it is interesting. It is more famously known for introducing the keywords async and await, which are currently the superstars of the asynchronous programming world.
This book is about using and understanding asyncio. It does give examples of using it but understanding is a less successful endeavour. There is no real framework underlying the explanations that indicates how to think about asyncio or how its creators thought about it. Asyncio is how it is because there are only a small number of ways of doing the job and this book does little to illuminate the situation.
Chapter 1 starts off with an explanation of threading and async using a story about the restaurant of threadbots. Personally I prefer explanations that target the technology. Explain how the problem arises in code rather than real life. I didn't like this chapter much, but you might disagree with me.
Chapter 2 is titled "The Truth about Threads" and it is a reasonable account of why threading is a problem in some cases. This isn't a deep explanation of anything much and there is much quoting of what other people say about what is good and bad about threads.
Chapter 3 is an Asyncio walkthrough that did little to explain anything much for me. You do get to look at some code and you do get a short explanation of coroutines and async await. What is missing is any deep explanation - what exactly is the relationship between a coroutine, task and future? When you do an await where does the main thread go? Is a future a promise and so on. Then there is a strange section where the ensure_future function is discussed as if it was mysterious and somehow inconsistent. Why does it return a future when the parameter is a coroutine and yet it returns a task, the same task, when the parameter is a task. Mysterious? No as the clue is in the name it ensures that the parameter is a future either by returning a coroutine wrapped in a future or returning a task which is already a future. At this point I was tempted to give up reading because the overall tone suggested that something, indeed many things, that were mystifying had been clarified - and they have not either because they weren't mystifying in the first place or they were and haven't been clarified.
I didn't give up at this point because the book finishes with Chapter 4 - did I mention that this is a short book? The final chapter goes though some examples and helps to confuse you even more if you aren't already confused enough.
This is a well written book in the sense that it is easy to read and seems to speak directly to the reader with a tone that suggests being on the same side. You get the impression that author has struggled with asyncio alongside the reader. Unfortunately there is only the slightest hint that he won the struggle. He clearly knows how to write programs that make use of asyncio, but I'm not sure that is enough qualification to explain the general ideas, and principles that lie behind - i.e. what is going on rather than just what you do. I found that as I continued to read, I understood less rather than more and this isn't what a book is supposed to do for you. Of course you may disagree, there are some useful examples of using asyncio and you might like the style. For me this book didn't help me understand asyncio as much as the official documentation did.
To keep up with our coverage of books for programmers, follow @bookwatchiprog on Twitter or subscribe to I Programmer's Books RSS feed for each day's new addition to Book Watch and for new reviews.
|Last Updated ( Tuesday, 19 May 2020 )|