TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Why Not Python - the GIL hinders concurrency

76 pointsby HerrMonnezzaalmost 12 years ago

20 comments

cdavidalmost 12 years ago
NumPy developer here. First, I agree that the &quot;the GIL is not an issue&quot; is an annoying meme. It can be an issue, and when it is, it is annoying as it complicates some architectures. I don&#x27;t think those architectures are as common as people usually think.<p>A few remarks: I would qualify the GIL as a tradeoff rather than a mistake. You loose CPU-bound parallelism with threading, but you gain easy to write C extensions (well, relatively speaking). I think this point is critical to the existence of something like NumPy (which is unrivalled in general PL AFAIK).<p>If you need to share a lot of data (a big numpy array), then you can use mmap arrays, there is no serializing&#x2F;desarializing cost, and that&#x27;s efficient. See for example some presentations by O. Grisel from scikits-learn (<a href="https://www.youtube.com/user/EnthoughtMedia/" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;user&#x2F;EnthoughtMedia&#x2F;</a> -- disclaimer, I work for Enthought, but all those video happened at scipy 2013).<p>IMO, the only significant niche where this is an issue is reaching peak performance on a single machine, but python is not really appropriate anyway -- that&#x27;s where you need fortran&#x2F;C&#x2F;C++, and even scala&#x2F;java&#x2F;haskell are quite unheard off there.
评论 #5987832 未加载
评论 #5987087 未加载
AnIrishDuckalmost 12 years ago
All of these problems can be addressed using inter-process shared memory. Shared memory support is built into multiprocessing [1].<p>Now I agree it might not be convenient, but that&#x27;s a matter of libraries. This post would be much more constructive if it was speculation on what such a library could look like, instead of pretending that concurrency &quot;doesn&#x27;t work&quot; in single-threaded Unix processes.<p>1. <a href="http://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.sharedctypes" rel="nofollow">http:&#x2F;&#x2F;docs.python.org&#x2F;2&#x2F;library&#x2F;multiprocessing.html#module...</a>
评论 #5986821 未加载
评论 #5987889 未加载
评论 #5986830 未加载
pwangalmost 12 years ago
Nice post, Chris, and it&#x27;s definitely a problem that many people are confused about (as the comments in this thread show). People who think the GIL is a problem either have no idea what they&#x27;re talking about, or they really know what they&#x27;re talking about. People who don&#x27;t think the GIL is a problem either have no idea what they&#x27;re talking about, or they really know what they&#x27;re talking about. :-p<p>One of the lesser-appreciated facts about the GIL is that <i>it is an implementation detail of CPython</i>. That is, it is entirely possible to implement a Python that does not have process-level globals and C statics. There is no <i>structural</i> reason why a C or C++ implementation of Python <i>needs</i> to have a GIL. It&#x27;s just a legacy of the implementation that has stayed around for a long, long time.<p>For instance, Trent Nelson has done some work to show that you can move to thread-local storage for the free lists and whatnot, and get nice multicore scaling, even with the existing CPython codebase[1]. There are still concurrency primitives that the language would need to offer at the C API level to manage the merger of these thread-local objects, but it&#x27;s a whole lot better than only being able to use a single core in modern days.<p>Fortunately I mostly get to work in the scientific field with (mostly) data parallel problems.<p>[1] <a href="https://speakerdeck.com/trent/parallelizing-the-python-interpreter-an-alternate-approach-to-async" rel="nofollow">https:&#x2F;&#x2F;speakerdeck.com&#x2F;trent&#x2F;parallelizing-the-python-inter...</a>
binarycrusaderalmost 12 years ago
<p><pre><code> But fundamentally, the GIL prevents Python from being used as a systems language. </code></pre> This is only true when severely restricting the definition of a systems language. The vast majority of command line utilities you&#x27;ll find in a typical operating system are often primarily I&#x2F;O-bound or do not have critical performance requirements.<p>As such, I&#x27;d only be willing to agree with the author&#x27;s statement for a very specific subset of systems programming.<p>For example, I&#x27;ve worked on a packaging system written in Python for the last five years or so. The package system is primarily I&#x2F;O-bound the vast majority of the time (waiting on disks or network), and almost all significant performance wins (some as much as 80-90%) have come from algorithmic improvements rather than rewriting portions in C (very little is written in C).<p>As one of my colleagues is fond of saying (paraphrasing), &quot;doing less work is the best way to go faster&quot;.<p>It also ignores the fact that depending on the problem space involved, there may be readily available solutions that provide excellent performance that don&#x27;t involve threading (e.g. the multiprocessing module, shared memory, etc.).
kevingaddalmost 12 years ago
This feels to me more like a critique of fork() and the unixy process-oriented parallelism model than a critique of Python. Of course, the author mentions this as a caveat, but it makes me wonder if the blame is being laid where it should be (and also whether in some scenarios like this, you should really just build applications the way applications are normally built for a platform, no matter how much you dislike it)
评论 #5986473 未加载
评论 #5987002 未加载
shanemhansenalmost 12 years ago
He has a valid point. There are certain types of workloads which don&#x27;t scale well on a single machine when writing pure python code. If your workloads happens to need to get more cpu performance out of a single machine in pure python and isn&#x27;t easily to parallelize using processes, python&#x27;s not the best choice. Personally I feel like go is good for this use case because I consider writing my own object lifecycle code a pita. Others will only use multithreaded c code, something I&#x27;m not eager to touch.<p>I do however feel the need to make a couple clarifications:<p>If you fork a process, you don&#x27;t necessarily duplicate memory. Yay for COW. Fork twice and you&#x27;ve got fanout with very little extra memory. Threading works fantastically for I&#x2F;O. C libraries can release the GIL during processing. So if you have to do something computationally expensive, you can let another python thread run.<p>I rarely feel restricted by python, and when I do I usually find that other aspects make up for it. I find it makes a decent systems programming language (although not as good as C). The os library and the ctypes library give you the capability to do most (all?) system level tasks.<p>I tend to prefer go over python when library support isn&#x27;t an issue, but that&#x27;s more due to it&#x27;s type system and channel primitives. As someone who&#x27;s pushed python to it&#x27;s limits as a systems programming language I&#x27;m pretty comfortable dropping down to C isn&#x27;t <i>often</i> called for, and writing in java&#x2F;scala isn&#x27;t ever required.
评论 #5987164 未加载
andrewguentheralmost 12 years ago
GIL doesn&#x27;t hinder concurrency, Python&#x27;s threading library is still concurrent. It just isn&#x27;t parallel.
评论 #5986845 未加载
falcolasalmost 12 years ago
This is just my opinion, but it&#x27;s served me well in the 8 or so years I&#x27;ve done Python development:<p>Why are you doing compute intensive work in Python? Python is not well optimized for doing compute intensive work. It&#x27;s made some strides over the years, being able to do limited bytecode optimization and multiprocessing, but it&#x27;s not a high performance computation language.<p>However, it is a fantastic glue language. Write your compute intensive portions in C, and use Python to glue the portions of C together. You can even do your thread generation in Python, release the GIL during your C execution, and you don&#x27;t even have to worry about the complicated process of spinning up threads in your C.<p>In other words, use the language like it was designed to be used.
评论 #5987944 未加载
评论 #5987453 未加载
评论 #5987847 未加载
senkoalmost 12 years ago
Timely and related talk given today at EuroPython conference about concurrency in Python and why (and when) GIL doesn&#x27;t matter: <a href="http://www.youtube.com/watch?v=b9vTUZYmtiE" rel="nofollow">http:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=b9vTUZYmtiE</a>
zzzeekalmost 12 years ago
&gt; Memory duplication has a relatively simple solution, namely using external cache such as redis. But the thundering herds problem remains. At time t=0, each process receives a request for f(new input). Each process looks in the cache, finds it empty, and begins computing f(new input). As a result every single process is blocked.<p>this is incorrect. The processes coordinate on a lock held in redis itself.<p>This solution is available right now using the Redis backend in dogpile.cache (of which I am the author): <a href="https://dogpilecache.readthedocs.org/en/latest/usage.html" rel="nofollow">https:&#x2F;&#x2F;dogpilecache.readthedocs.org&#x2F;en&#x2F;latest&#x2F;usage.html</a>
评论 #5987110 未加载
评论 #5986925 未加载
ditadosalmost 12 years ago
no mention of gevent, celery, etc. As someone who runs thousands of concurrent tasks in a mix of process&#x2F;gevent (one UNIX process for each 100 greenlets across 48 cores on two boxes), I find the OP&#x27;s toliling rather misguided.
评论 #5987035 未加载
yasonalmost 12 years ago
<i>The standard Python workaround to the GIL is multiprocessing. Multiprocessing is basically a library which spins up a distributed system running locally - it forks your process, and runs workers in the forks. The parent process then communicates with the child processes via unix pipes, TCP, or some such method, allowing multiple cores to be used.</i><p>Multiprocessing is <i>the way</i> to do parallelism. Deviating from that should be an exception -- for example, shared memory maps could be used to transfer select data objects instantaneously between the processes instead of serializing&#x2F;deserializing over a pipe, and only those while still retaining separate process images. Threads were practically invented as a compensation for systems with heavy process image overhead.<p>I think Python is very Unix in this regard. And that&#x27;s not a bad thing per se. Unix and Linux can do multiprocessing very efficiently.
评论 #5986831 未加载
评论 #5986905 未加载
评论 #5986705 未加载
VeejayRampayalmost 12 years ago
s&#x2F;Python&#x2F;Ruby. Title still works.
3amOpsGuyalmost 12 years ago
How can the GIL, which is restricted to one process, hinder concurrency? It can only impact 1 single form of concurrency, threading.<p>Why use threads?<p>Noone does parallel compute on CPUs these days, not since GPGPUs rocked up almost 5 years ago (and we often use python as the host language, thanks pyCuda!)<p>Parallel IO then? Well, except that async IO is often far more resource efficient (at the cost of complexity though).<p>Threading is dead(-ish) because its hard to write, hard to test and expensive to get right.<p>Concurrency in python is very much alive though.
评论 #5986900 未加载
评论 #5987975 未加载
评论 #5990260 未加载
thezilchalmost 12 years ago
&gt; The implementation would also likely be considerably more complicated than the 160 linues of code that the Spray Cache uses.<p>Not likely, using Twisted deferreds and a sane cache-wrapper with herd awareness -- you probably want this regardless of long-running cachables.<p>Of course, Python 3.2 also has a futures [thread or process] builtin, if that&#x27;s your thing.<p>10-20 lines of code.
minimaxalmost 12 years ago
I don&#x27;t get how having multiple copies of a quote hanging around in memory is a big problem. It&#x27;s probably less than 100 bytes total (symbol + side + price) and probably significantly smaller than the size of the state associated with the &quot;statistics process.&quot;
评论 #5988303 未加载
halaylialmost 12 years ago
if you need performance, just don&#x27;t use Python. You&#x27;ll be better off using C&#x2F;C++.
评论 #5987335 未加载
Nursiealmost 12 years ago
Regardless of other arguments, it basically means that threads in python become only a way of thinking about a problem rather than a way yo utilise hardware fully. It&#x27;s a shame.
pdpialmost 12 years ago
The guys at CCP (the makers of EVE Online) seem to be doing just fine with parallelising stuff in Python.
评论 #5986916 未加载
MostAwesomeDudealmost 12 years ago
The author appears to not be aware of where the big leagues are. Additionally, as is becoming a recurring theme here, he doesn&#x27;t know about PyPy nor Twisted. This is continually disappointing.
评论 #5986806 未加载
评论 #5986736 未加载
评论 #5987959 未加载