6

Why is it that you can run Jython and IronPython without the need for a GIL but Python (CPython) requires a GIL?

bcat
  • 8,833
  • 3
  • 35
  • 41
JamesD
  • 77
  • 1
  • 3
  • 2
    See http://stackoverflow.com/questions/991904/why-is-there-no-gil-in-the-java-virtual-machine-why-does-python-need-one-so-bad/991917#991917 . – Alex Martelli Aug 07 '10 at 05:05
  • 1
    @Alex Martelli, that post does infact asked the same question BUT no answer explains WHY cpython requires a GIL. – JamesD Aug 07 '10 at 05:12

2 Answers2

11

Parts of the Interpreter aren't threadsafe, though mostly because making them all threadsafe by massive lock usage would slow single-threaded extremely (source). This seems to be related to the CPython garbage collector using reference counting (the JVM and CLR don't, and therefore don't need to lock/release a reference count every time). But even if someone thought of an acceptable solution and implemented it, third party libraries would still have the same problems.

Note that extensions written in C can in fact get rid of the GIL: http://docs.python.org/c-api/init.html#thread-state-and-the-global-interpreter-lock

3

My guess, because the C libraries that CPython is built upon aren't thread-safe. Whereas Jython and IronPython are built against the Java and .Net respectively.

Alan
  • 45,915
  • 17
  • 113
  • 134