Python that blocks 6% of the processor?

I have a program that uses this library , basically does something very simple, like this

receiver = multicast.MulticastUDPReceiver ("192.168.0.2", symbolMCIPAddrStr, symbolMCPort ) while True: print 'Spinning' try: b = MD() data = receiver.read(1024) 

The receiver socket is locked until data arrives, so print 'Spinning' prints only once until the data is received on the socket. When I ask the OS how much CPU this process takes, even if it is waiting to be received, it returns with:

 [ idf@node1 ~]$ ps -p 4294 -o %cpu,%mem,cmd %CPU %MEM CMD 6.3 0.4 python ./mc.py -s EUR/USD [ idf@node1 ~]$ 

In fact, if I run several of these processes, my computer with two CPUs and 8 cores each, all cores go for 100% use, and the computer becomes unusable.

I must misunderstand the concept of python about โ€œlockingโ€, because even doing nothing, which basically needs to sleep, takes a lot of processor.

Is there a more correct way to write this so that programs that are mostly waiting for I / O [with interruptions] refuse the processor?

+6
source share
1 answer

You have not published a complete example, so itโ€™s hard to say whatโ€™s going on.

However, I see that there is a try block inside the loop, and your network code is inside the try block. I do not know what your exception handling does. However, I suppose this makes something like inadvertently swallowing an important error. Then your loop starts again and probably generates the same error. Thus, the program is actually busy with the loop, even if you thought it was sleeping, blocking I / O.

0
source

All Articles