6

Maybe there's someone out there with the right interests that will know how to answer this. Basically the question is: What are the differences between the multiprocessing module in Python, and the parallelism in Haskell. For instance: are threads created in Python mapped to OS threads? If so, what if there are more threads than cores? Are they multiplexed into the OS threads? Who schedules these threads? Thanks for all the info: documentation/insights greatly appreciated.

Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
  • 1
    GHC Haskell's threading model is described in this question: http://stackoverflow.com/questions/5847642/haskell-lightweight-threads-overhead-and-use-on-multicores/5849482#5849482 – Don Stewart Jul 05 '11 at 19:57

2 Answers2

7

As opposed to Python (See Eli's answer), the threading model of Haskell is quite different. You have a difference between concurrency (multiple threads handling different aspects of the program) and parallelism (multiple threads just to speed up computation). Both are handled by lightweight threads managed by the Haskell RTS. In the second case, one can use primitives like pseq and par that hints the runtime to do the calculation in another thread, if this gives an advantage. The runtime automagically decides this.

Community
  • 1
  • 1
fuz
  • 88,405
  • 25
  • 200
  • 352
6

The Python multiprocessing module has nothing to do with threads. It tries to provide an API similar to threading (which does expose threads) with processes underneath.

Python threads are mapped to OS threads, and if you create more threads than cores the exact same thing happens as if you'd do it in C (pthreads, Win API threads, etc.) - the OS juggles the threads between cores.

There's a lot of information online about Python threads, just Google it.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Thanks, Eli: "just Google it". I was wondering if you could be a little more specific. There's just too much (and spread out) info for me to digest, hence the question. Maybe you can help me by pointing me to the *best* documentation? Also, I'm interested in comparing Python to Haskell, more than just Python itself. Thanks. – Dervin Thunk Jul 05 '11 at 18:52
  • It should be noted, that multi-threading in CPython is hindered by the [GIL](http://en.wikipedia.org/wiki/Global_Interpreter_Lock). Thus, multi-threading under (mainstream) Python is used primarily as an expensive means to concurrency (and not so much parallelism). There are also some third-party concurrency facilities, such as [greenlets](http://packages.python.org/greenlet/) which resemble a bit more Haskell's microthreads on a single core. – hvr Jul 05 '11 at 20:10
  • @hvr: the "multiprocessing" module says it "effectively side-step[s] the Global Interpreter Lock". Care to expand on this? See http://docs.python.org/library/multiprocessing.html – Dervin Thunk Jul 05 '11 at 21:46
  • @Dervin: I think the point about sidestepping the GIL is that it uses multiple *processes* not multiple *threads*. The GIL will prevent multiple threads running concurrently, but there is only one GIL per process so using multiple processes is sidestepping the GIL problem. –  Jul 05 '11 at 22:00
  • @Dervin: here's a good article to read: http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/ in addition, go over the links listed here: http://diveintopython3.org/where-to-go-from-here.html (in the *Threading & Multiprocessing* section) – Eli Bendersky Jul 06 '11 at 04:26
  • Great. Thanks for all the info, Eli. – Dervin Thunk Jul 07 '11 at 20:05