Will multithreading make python less popular?
Christof Donat
cd at okunah.de
Mon Feb 16 10:54:10 EST 2009
Hi,
> But there is
> something interesting, something like multi processing. But is it a
> real alternative for multi threading. As i searched it is not, it
> requires heavy hardware requirements (lots of memory, lots of cpu
> power).
Not necessarily.
For the memory, modern operating Systems can ustilize a copy on write
semantics in their Applications virtual memory space. That means that after
starting a new process with fork(), the new Process shares all physical
Memory Pages with the original one as long a none of them writes to wome
Memory. The first write is cought by the OS. Then the Page will be copied
and the writing process will in future use the new copy of that page while
the other one (or eventually many) stay with the original. Multiprocessing
needs more Memory than well optimized Multithreading, but it is not as bad
as it is often said.
For the CPU: There are two things that make the difference here.
One ist Context Switching. When the OS switches between two threads of the
same Process, it does not need to change the memory lookup table. When
switching between Processes that is always necessary. Switching from a
Thread of Process A to a Thread of Process B is just like switching from A
to B.
Using Threads just increases the probability that the OS can do a faster
Context Switch. So Threads are faster here, but it is not a too big issue as
well.
The Second Thing is Communication. With Threds you can simply use variables
to communicate between Threads. To Prevent conflicting access to those
Variables you will use e.g. mutexes. That can be done with Shared Memory as
well. Shared Memory is a bit more difficult to handle than simply using the
heap, but again the impact is not really so big.
Google uses Processes for the Tabs in Chrome, because that way they get
around many Memory Management Problems they would have with Threads or with
a singlethreaded reactor. Using Processes is not per se a bad Idea. You pay
a bit with Memory and CPU but in many situations you get a much simpler
programming model.
Christof
More information about the Python-list
mailing list