Learning Python Network Programming

(Sean Pound) #1
Chapter 8

With this in mind, we're going to use multithreading rather than multiprocessing
in our echo server. The shared data model will simplify the code that we'll need for
allowing our chat clients to exchange messages with each other, and because we're
I/O bound, we don't need processes for parallel computation. Another reason for
not using processes in this case is that processes are more "heavyweight" in terms of
the OS resources, so creating a new process takes longer than creating a new thread.
Processes also use more memory.


One thing to note is that if you need to perform an intensive computation in your
network server application (maybe you need to compress a large file before sending
it over the network), then you should investigate methods for running this in a
separate process. Because of quirks in the implementation of the GIL, having even
a single computationally intensive thread in a mainly I/O bound process when
multiple CPU cores are available can severely impact the performance of all the I/O
bound threads. For more details, go through the David Beazley presentations linked
to in the following information box:


Processes and threads are different beasts, and if you're not clear on
the distinctions, it's worthwhile to read up. A good starting point is
the Wikipedia article on threads, which can be found at http://
en.wikipedia.org/wiki/Thread_(computing).
A good overview of the topic is given in Chapter 4 of Benjamin Erb's
thesis, which is available at http://berb.github.io/diploma-
thesis/community/.
Additional information on the GIL, including the reasoning behind
keeping it in Python can be found in the official Python documentation at
https://wiki.python.org/moin/GlobalInterpreterLock.
You can also read more on this topic in Nick Coghlan's Python 3 Q&A,
which can be found at http://python-notes.curiousefficiency.
org/en/latest/python3/questions_and_answers.html#but-
but-surely-fixing-the-gil-is-more-important-than-
fixing-unicode.
Finally, David Beazley has done some fascinating research on the
performance of the GIL on multi-core systems. Two presentations of
importance are available online. They give a good technical background,
which is relevant to this chapter. These can be found at http://
pyvideo.org/video/353/pycon-2010--understanding-
the-python-gil---82 and at https://www.youtube.com/
watch?v=5jbG7UKT1l4.
Free download pdf