chapter three

3 Concurrency, parallelism and asynchronous processing

 

This chapter covers

  • Using asynchronous processing to allow efficient response to user requests
  • Understanding threading in Python, especially its limitations
  • Making multi-processing applications that can take full advantage of multi-core computers

[The good news is that] modern CPU architectures allow for more than one sequential program to be executed at the same time, permitting [impressive processing speeds. In fact, speeds can increase right up] to the number of parallel processing units (e.g. CPU cores) that are available. [The bad news is that to take advantage of all this parallel processing speed, we need to write parallel code, and Python is ill-suited for writing parallel code.] Most Python code is [sequential], so it is not able to use all the available CPU resources. [In other words, our usual python code cannot make use of] modern hardware’s capabilities, and our solution will run at a much lower speed than the hardware allows. So we need to devise techniques to help Python make use of all the available CPU power. In this chapter we are going learn how to do just that, starting with some approaches you may be familiar with in general, but which have some unique Python twists. We will discuss concurrency, multi-threading and parallelism the Python way, including some strong limitations around multi-threaded programming.

3.1 Writing the scaffold of an asynchronous server

3.1.1 The implementation of the scaffold for communicating with clients

3.1.2 Programming with coroutines

3.1.3 Sending complex data from a simple synchronous client

3.1.4 Alternative approaches to interprocess communication

3.2 Implementing a basic MapReduce engine

3.2.1 Understanding MapReduce frameworks

3.2.2 Developing a very simple test scenario

3.2.3 A first attempt at implementing a MapReduce framework

3.3 Implementing a concurrent version of a MapReduce engine

3.3.1 Using concurrent.futures to implement a threaded server

3.3.2 Asynchronous execution with Futures

3.3.3 The GIL and multi-threading

3.4 Using multi-processing to implement MapReduce

3.4.1 A solution based on concurrent.futures

3.4.2 A solution based on the multiprocessing module