8 ms·
'We are no longer as optimistic about removing the GIL completely. '
- antirez 17y agoInstead of dealing with all this complexity, I don't understand why a simpler approach is not used, like having a single interpreter per thread and a very good message passing strategy between interpreters.
- mahmud 17y agoYou could already achieve that with OS processes and IPC. The whole point of having multi-threading is to be able to write compact, shared-memory code with minimal use of synchronization operators, and sharing as much code and data as possible. One interpreter per-thread means all side-effects have to be migrated to the other threads to keep a consistent view of memory: guess what you will need to do that? yep, a global lock (except this time it's across all interpreters, instead of just one.)
- antirez 17y agoThe whole point of the "Smart message passing" strategy is to move objects from one thread to another one very fast (that is, zero-copy), and with very local locking requirements.
- yummyfajitas 17y agoIf you restricted shared memory to objects explicitly declared as shared, you wouldn't need a GIL. You'd simply need per-object locks. For scientific computing purposes, you can often accomplish this with multiple processes and a numpy array allocated by shmget/shmat. But I'm not sure how to share complex objects in this way.
- nailer 17y agoI'm not quite sure if I'm right here (and I'd appreciate it if another HN reader corrected me). But I think that's how the Queue object in Python 2.6 works. The Queue instance is locked, you seem to be free to do whatever within the threads that are consuming the queue. The reason I'm not sure is that having a single object being locked seems to contradict the GIL concept...
- dkersten 17y ago...and if we moved away from the annoying-and-awkward shared memory model, then this becomes even less of a problem still.
- thwarted 17y agoLike perl's ithread implementation? I like perl's ithread setup a lot. You explicitly need to mark data as shared between threads, otherwise all variables/objects are local to the thread. Things like queues, for example, are implemented as shared @arrays with a locking primitive around the manipulations, mostly hidden behind the Queue API (Queue->enqueue and Queue->dequeue). The interpreter code is still shared among all the threads, but each has thread local storage separate from the shared arena. I've found that explicitly needing to mark data as shared to be a big boon to development, I think it has helped reduce the number of the bugs related to shared state.
- FooBarWidget 17y agoLast time I used ithread it made Perl crash and burn. This was on program with about 30k lines of code. On top of that, creating a single thread takes 15 seconds because Perl is spending a ridiculous amount of time copying the entire state to the new thread. Eventually I used fork() and multiprocessing because that's much faster than using ithread. ithread is pretty useless.
- thwarted 17y agoThen you must have used them a really long time ago. The last time I did, I had no trouble decoding audio streams in one thread, pushing the raw audio samples into a Queue, pulled the raw audio samples out of the Queue in another thread and sent them to the audio device, and doing network IO in another thread. This code is at http://github.com/thwarted/thundaural http://github.com/thwarted/thundaural (no longer maintained) and was written in 2004 and 2005 against perl 5.6 and perl 5.8. It's hardly a complex or large threaded application, nor an example of fantastic code, but I never experienced "crash and burn" or prohibitively long thread start up time. I can't produce a significantly long start up time when perl copies a 240meg+ local state using http://gist.github.com/258016 http://gist.github.com/258016. Much larger, and the overhead pushes my 4gig machine into swap (so perl's not very efficient with its data overhead, it seems). It is slower than if the structure is shared (you can uncomment a line to compare) between the threads, which shows that perl is copying the state to the other interpreters, but this just says you need to use the same model with perl threads that you'd use when you do multiprocessing with fork if you're going to have long lived child processes that don't exec: spawn threads early before your heap balloons to avoid all that spawn-time copying (which is a good practice anyway, even when COW is used for fork). Every model has its trade offs, and it's good to know those trade offs going in, rather than treating the flavor of the day as a silver bullet. And even if perl's implementation isn't stable/good, it doesn't mean the model of explicit sharing is a bad one. And it most definitely doesn't mean that using a separate interpreter in each thread with global sharing is bad at all.
- gaius 17y agoThis is how Tcl works.
- antirez 17y agoYes, probably Tcl lacks some zero copy object passing mechanism IIRC.
- psranga 17y agoTcl has reference counting and copy on write to minimize copies when passing objects to functions. Not sure if this is the case for slave interpreters (Tcl's term), but I would be very surprised if it were not. http://tmml.sourceforge.net/doc/tcl/Object.html http://tmml.sourceforge.net/doc/tcl/Object.html http://www.equi4.com/moam/strength http://www.equi4.com/moam/strength
- silentbicycle 17y agoLua, too, though there are a couple extensions to add threads.
- j_baker 17y agoYou mean like the multiprocessing package? http://docs.python.org/library/multiprocessing.html http://docs.python.org/library/multiprocessing.html
- draegtun 17y agoNo because IIRC the Python multiprocessing package uses fork rather than threads.
- mahmud 17y agoIn Linux they're identical :-)
- duskwuff 17y agoNoo... fork() gives you a separate address space, whereas threads would require that you share an address space with the parent. vfork() and clone() can almost give you what you want, but Linux threading is using something different (NPTL) nowadays.
- mahmud 17y agoYou're correct. My position was that in Linux, threads weigh just as much as processes; the forking speed is identical, and extremely fast with COW. Compared to other OSes where processes and threads are completely different beasts. Even with NPTL, both processes and threads have task_structs.
- thwarted 17y agoNPTL uses clone(2). fork(2) is implemented in terms of clone(2) also.
- nas 17y agoOn plaforms without fork it uses threads (e.g. windows). Why do you care if it is using fork() anyhow?
- zepolen 17y agoWhy are real threads so important? Does anyone have an example where threads would be much better than using the multiprocessing module?
- heresy 17y agoBlocking operating system calls? This has always been the problem I've run into with green threads.
- parbo 17y agoI think the GIL is released during most of those, so that should already work with real threads.
- j_baker 17y agoNote that Python doesn't use green threads. It uses real, native threads. The thing is that the interpreter will prevent any other threads from accessing it until the GIL is released.
- nas 17y agoThe problem with the GIL is nearly the opposite. The lock is dropped for system calls but kept when Python code is running. That's not a problem for IO bound workloads but does not scale well over multiple CPUs for CPU bound tasks that do most of the work in Python-land. I not sure dropping the GIL is the best solution. Something like Erlang message passing sounds like a better model. I'm not sure if that's a good fit either though since it works best with functional style programming.
- mahmud 17y agoWith native threads, all your threads have their own identity and they're known to the OS task scheduler. So they can all block or run independently. But with green threads, the OS doesn't know about your "threads"; when the parent process is blocked, so are all the threads.
- stonemetal 17y ago
- ghshephard 17y agoMarch 2009, presuming the comments followed the creation of that article.
- ZeroGravitas 17y agoNo, I remember reading those comments before this revision. I was under the impression that these Google code wiki pages were kept under source control so you should be able to view histories etc. but I can't see any obvious link. Found it, the change to this section was done 33 hours ago, a diff can be found here: http://code.google.com/p/unladen-swallow/source/detail?spec=svn937&r=935 http://code.google.com/p/unladen-swallow/source/detail?spec=...
- smikhanov 17y agoI don't want to put professional qualities of Google engineers under doubt, but could someone explain to me why it's so complicated to remove GIL in Unladen Swallow? As far as I remember, UnSw targets LLVM which is an advanced VM with JIT compilation (i.e. not fully interpreted). Jython does (I'm not a Jython developer, so may not know the details) nearly the same, but targets the JVM and Jython does not have GIL as a result. What's the key difference between LLVM and JVM in this regard?
- cconstantine 17y agoThe simple answer is that LLVM does not come with a garbage collection system, and the JVM does.
- sandGorgon 17y agosnigger - first clojure and now jython. And the news about high profile websites moving to Lift/Scala from <whatever>. Is the JVM the answer to life, the universe and everything ?
- fauigerzigerk 17y agoWell, the JVM is a high quality, mature, very fast, cross platform runtime. However, I'm always slightly concerned that letting the JVM in through the front door might open the back door to the kind of SOP (Soviet Oriented Programming) exemplified by Java EE. JSR hell is lurking behind the most innocent-looking serverside library :-)
- smikhanov 17y agoI can't see how JSR argument is applicable here. Java suffers from JSR approval hell just because it tries to stadardize everything. This is the requirement to perform well in the enterprise world, along with the programmers certification mechanism and inclusion of the top industry players in the standards approval. If your language is just a hackers' toy (Clojure and Scala are exactly that) then why do you care about JSRs in the first place? The JSRs don't mention JVM usually, only the libraries or Java language extensions.
- axod 17y agoHow about teaching programmers to program without using threads? edit: sure downmod me. It's crazy talk! How could programmers do without threads and concurrency issues and all of the other blocking problems. Hardware should handle multiple cores. Not programmers.
- bioweek 17y agoSounds like a good idea. I'd upvote you but I can't seem to vote on comments for some reason. Too young, or new?
- silentbicycle 17y agoIt looks like your karma isn't high enough.
- andrew1 17y agoI think you're being downvoted because people disagree with what you're saying. In my experience you need multiple threads when you want multiple things to happen at the same time. i.e. if I have a client/server architecture and one client instructs the server to perform a long running task then I don't want the server to appear frozen to all my other clients, which it would if the server ran in a single thread. I don't really see how you can get around this. Do you have a solution?
- j_baker 17y agoThis prompted me to ask this question on stackoverflow: http://stackoverflow.com/questions/1914605/what-does-pythons-gil-have-to-do-with-the-garbage-collector http://stackoverflow.com/questions/1914605/what-does-pythons... I'd like it if someone could show me how the garbage collector is related to removing the GIL.
- Tuna-Fish 17y agoGlad to be of service. ;)
- silentbicycle 17y agoPython's garbage collector itself isn't "thread safe", so removing the GIL without replacing the GC would cause major problems.
- ig1 17y agoI'm no expert in the GIL, but pretty much every widely adopted Garbage Collection algorithm requires a "stop-the-world" phase where object references can't be changed. Every VM has some concept of "stop points" where all user code is suspended, but Python's GIL is much more wide-ranging than that found in say the JVM or .NET.
- Tuna-Fish 17y agoPython doesn't really have real GC, it refcounts. See the stackoverflow answer.
- jemfinch 17y agoPython refcounts and it has a real mark+sweep collector for collecting cycles. It's not a dichotomy, you know.
- silentbicycle 17y agoThe reference counting causes the problem here. When multithreading means that incrementing/decrementing a reference count is no longer deterministic, you need locks, or all hell breaks loose. Adding a mark&sweep GC isn't going to fix that. While I'm not familiar with the specifics of Python's GC, a mark&sweep phase is usually added to reference counting so that if there's garbage which contains references to itself but has no external references, it will eventually be collected. (_Garbage Collection_ by Richard Jones and Rafael Lins is an excellent resource on GC details, btw. There's also a decent overview in the O'Reilly OCaml book (http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora082.html http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora082.... )). In other words, it plugs the worst memory leaks caused by reference counting. How to do multiprocessor / multithread GC well is still an area of active research. In the mean time, one simpler solution is to have several independent VM states, each running in their own thread (or process), and communicating via message passing. Lua makes this easy, but its VM is considerably lighter than Python's.
- nihilocrat 17y agoAt least they want to get rid of the horrible abomination known as reference counting. It's the primary reason why I've moved on to other languages for the sake of creating games.
- euroclydon 17y agoIf Python can't get this threading thing worked out, isn't the language going to get left behind as parallel architecture marches onward?
- cdavid 17y agoThere are many ways to exploit multi-cores, multi-threading is just one of them. Many other techniques exist. Also, one thing to realize is that if speed really matters (like in scientific apps), you will get much higher speed increase by rewriting some parts in C than by allowing using all the cores from python (at least with only a couple of cores). Finally, a point which is not often brought but is crucial in my opinion is about C extension: the GIL makes C extensions much easier to write. That's one big reason for python success in the first place.