This is crazy! I heard Python threads are slow but this is beyond normal.
Here is the pseudo-code:
v = 
def __init__(self, threaded = True):
self.v = MySocket('127.0.0.1')
t0 = datetime.now()
t = datetime.now()
dt = (t-t0).total_seconds()
r = ReadThread(threaded = False)
r = ReadThread(threaded = True)
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: pass
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) time.sleep(60)
What will be the measuring in that case then?