alasarr
alasarr

Reputation: 1605

Multithread application hangs when one thread takes a 100% of CPU

The application has been written in Python 2.7 and running on Ubuntu 14.04.

It runs 50 threads, one thread takes all the CPU (100% usage is shown in htop) and the others never have the chance to continue executing. It looks like the dispatcher is not able to change to other threads. Is that normal?

Should I try using processes instead of threads? I'm thinking that because using multiprocessing I could use more CPUs.

Upvotes: 0

Views: 1447

Answers (2)

user3159253
user3159253

Reputation: 17455

https://wiki.python.org/moin/GlobalInterpreterLock

And yes, multiprocessing is possibly the recommended way to implement things in your case. Or you may try to rewrite the hanging thread so that it won't take much CPU (depends on why it hangs, likely it counts something). Or at least run time.sleep(0) from time to time as @user5402 suggests.

Also you might find useful asyncio

Upvotes: 1

ErikR
ErikR

Reputation: 52029

If you know which thread is hogging the cpu, try inserting

time.sleep(0)

somewhere in its main loop. This will cause it to yield to another available thread.

What kind of work is that thread doing? Is it doing any IO? Python threads work best on IO-bound tasks. For CPU-bound tasks it is possible it is never allowing another thread to run.

Upvotes: 2

Related Questions