Live data from Hacker News

Threading in Python

nryoung.org

11–20 of 30 posts

Re: Threading in Python

#12
post #9

Earlier quoted context omitted.

It still provides concurrency.

Concurrency itself is not desirable. It is a means to end such as performance.

This turns out not to be the case - some problems and calculations are most naturally expressed with concurrency.

Re: Threading in Python

#13
> Generally, you should only use threads if the following is true: - Sharing memory between threads is not an issue.

Here's the problem. Threads are really useful only if you can share memory between threads. If you can't share memory, you're usually better off using many processes.

Threads in Python (ie. CPython) can still be useful for I/O multiplexing or executing native code in background worker threads via FFI and releasing the GIL while doing so. For I/O multiplexing, there are better options than Python threads (select/poll/kqueue/epoll system calls and frameworks like twisted that use them).

In most applications, threads probably should not be used in CPython/CRuby code as they provide little performance gain compared to the complexity and overhead they add.

Re: Threading in Python

#14
post #13

> Generally, you should only use threads if the following is true: - Sharing memory between threads is not an issue. Here's the problem. Threads are really useful only if you can share memory between threads. If you can't share memory, you're usually better off using many processes. Threads in Python (ie. CPython) can still be useful for I/O multiplexing or executing native code in background worker threads via FFI a…

http://en.wikipedia.org/wiki/Communicating_sequential_proces... works well passing immutable object graphs back and forth. Passing by value (copying everything) has a cost, and serializing everything down to byte streams across a pipe is even more expensive, especially if you don't know which portions of the object graph will and won't be needed for a given call (an optimization which imposes tight coupling on details about the code you're calling). If I'm not calling untrusted code, and not planning to divide the work across many machines, I'd prefer to avoid needless process boundaries.

Re: Threading in Python

#15
post #9

Earlier quoted context omitted.

Concurrency itself is not desirable. It is a means to end such as performance.

This turns out not to be the case - some problems and calculations are most naturally expressed with concurrency.

Could you provide an example of a calculation that is more naturally expressed with concurrency?

Re: Threading in Python

#18
post #9

Earlier quoted context omitted.

It still provides concurrency.

Concurrency itself is not desirable. It is a means to end such as performance.

> Concurrency itself is not desirable.

Sorry, but you this is misguided. You are almost certainly using an OS that gives you concurrency, even if you only have one core of execution on your cpu. Concurrency is only natural, and is in fact a requirement when you start talking about GUIs. Even for something like data processing, you usually have a thread doing the processing, and another thread controlling everything. The advantage of threads is the natural separation of tasks, simplifying how programs are written. Not having the advantage of performance in python is unfortunate, however this is only one use for threads, which is by far not the most common.

Re: Threading in Python

#20
post #13

> Generally, you should only use threads if the following is true: - Sharing memory between threads is not an issue. Here's the problem. Threads are really useful only if you can share memory between threads. If you can't share memory, you're usually better off using many processes. Threads in Python (ie. CPython) can still be useful for I/O multiplexing or executing native code in background worker threads via FFI a…

Thank you. I would even go so far as to say that except in simple cases (downloading 10,000 images goes much faster with 100 worker threads than serially - which is I think the origin of "dont share memory) I would say do not use Python - or any other similar language.

Got parallel needs at your core? Look at Erlang or Haskell. If parallel or distributed work is mission critical, go with a language that has such things at its very soul. Python is a great language, but it is being enthusiastically bent to do things it is not top of the class for.

Want to handle more concurrent connections per python web server? If WSGI in Gunicorn is not enough, stop trying and use a load balancer to spread work between more servers.

Post reply on HN