Carlos Carlos - 5 months ago 42
Python Question

Same socket speed is 66% slower on a new thread?

This is crazy! I heard Python threads are slow but this is beyond normal.

Here is the pseudo-code:

class ReadThread:

v = []
def __init__(self, threaded = True):
self.v = MySocket('')
if threaded:
thread.start_new_thread(self._scan, ())

def read(self):
t0 ='SomeVariable')
t =
dt = (t-t0).total_seconds()

print dt

def _scan(self):
while True:

If I run the read() in a while loop in the main-thread as this:

r = ReadThread(threaded = False)
while True:

dt is about 78 ms with small variation. Now if I run it in a new thread like this:

r = ReadThread(threaded = True)
while True:

dt is about 130 ms with +-10ms variance!

Why is it so slow? Am I doing something really wrong? It's the same thing just in a new thread!

MySocket() is is an object that uses a socket to read/write variables to a server and read() just gets some variable for the test.

Answer Source

It is hard to reproduce this problem locally without knowing what MySocket is, and the full example. However, I can try guessing, that the problem in this cycle:

while True:

It is VERY CPU-consuming. The CPU literally goes around all the time, taking the CPU cycles to itself, and not letting the socket to work.

Contrary, the socket read operations are usually blocking and idling for the data to arrive, so they consume almost no CPU.

In the first example, you run your socket while nothing else eats CPU. In the second example, the main thread consumes 1 CPU completely.

Try replacing this cycle with a usual idling operation, e.g. time.sleep(60). So the main thread will idle for 60s while the socket thread will read and process data.

r = ReadThread(threaded = True)

What will be the measuring in that case then?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download