Note: you can try this tutorial in Binder.

What is concurrency

Aiochan is a python library for CSP-style concurrency in python. So what is concurrency? Why do we need concurrency? If the usual python programs that we are familiar with are “non-concurrent”, aren’t we doing just fine?

By far the most important justification for the need of concurreny is that concurrency enables our programs to deal with multiple, potentially different, things all at once. And no, we are not doing fine without concurrency. For example, suppose you are writing a webserver. Let’s say that your code goes like this:

while True:
    parse request from client
    do something with the request (which takes time)
    return the result to the client

This is all very good. It works, but if this is the outmost loop of your server, then it is obvious that at most one client can be served at any one instant: clients effectively come in by a queue, and one can only be served when the previous one is done. If a client requires doing an operation that takes, say, ten minutes to complete, then the other clients will not be too happy.

“I have been writing python webservers using non-concurrent codes not too different from above for a long time, and it is definitely not true that only one client is served at any one instant”. Well, most likely you are using some web frameworks and it is the framework that controls the outmost loop. In other words, your framework is managing all your concurrency whilst presenting a non-concurrent façade to you. By learning to write concurrent programs yourself, not only will you have the ability to fix your webserver when things go wrong (most often it is not bugs, but instead you are not honouring your framework’s assumptions about what your code is supposed to be doing), but a great many opportunities of novel software architectures and applications also open up to you. The potential of building something greater than everything that comes before is what we have been always after, no?

So, concurrency means the ability to “deal with multiple things at once”. Let’s say that you have all the things you need to do written on a to-do list. And you are only a single person. Can you still have concurrency in completing your to-do list? Yes. Concurrency only means that you do not need to take the first item off the list, do it and wait for the result, then start with the second item. Your first item might well be “watch the news at 9am, then watch the news at 9pm, and find out what new things have happened since 9am”. In this case non-concurrent behaviour means sitting there doing nothing after watching the 9am news until 9pm. As long as you switch context and starting doing something after the morning news you are in the realm of concurrent behaviour. You need neither a group of cronies to whom you can delegate your news watching, nor the rather unusual ability to watch two programs at once and perfectly understanding both, to concurrently perform tasks. Having a group of cronies doing stuff for you is a form of parallelism. Concurrency enables parallelism (it is useless to have many cronies if you need to wait for any one of them to complete their work before assigning work to the next one), but parallelism is not necessary for concurrency. Parallelism usually (but not always) makes things go faster. Concurrency can also make things go faster even without parallelism. In the case of computers, you do not need to have multiple processors to benefit from concurrency.

Now back to our webserver example. How do you write a concurrent outmost loop then? By analogy with the todo list example, you want somehow switch context and continue with the next client when you cannot fulfill the client’s request immediately. It is a good exercise to try to implement it yourself before continuing.

Ok, back with your concurrent webserver implementation? Well, if you have not seen anyone else doing it before, you are probably still thinking in quite concrete terms like “requests”, “accesses to databases”, “file reading and writing” (if these are the things your webserver is doing), and trying to make these concrete tasks concurrent will feel like juggling ten balls at once – the solution is brittle, and you probably won’t enjoy doing them everyday.

Here is the place for concurrent libraries, frameworks, or language constructs: they provide you with concurrent abstractions, and as long as you follow certain rules, they enable you to have the benefit of concurrency without the need of professional juggling training. Aiochan is such a library.

To recap:

  • Concurrency enables you to deal with multiple things at once.
  • Concurrency has the potential to decrease latency and increase throughput.
  • Concurrency is not parallelism but enables it.
  • Concurrency frameworks, libraries and language constructs provide abstractions that enable you to take advantange of concurrency without writing complicated and brittle code.