为什么子类化Thread比调用Thread慢得多?

时间:2019-04-27 23:46:52

标签: python python-2.7 python-multithreading

我有Python 2.7代码,该代码发出1,000个请求以检查我当前的IP地址。我已经使用200个线程和套接字编写了它。该代码使用两种方法执行相同的任务。除了一个子类threading.Thread

之外,我在下面的两者之间找不到任何区别
#!/usr/bin/env python2.7
import sys, ssl, time, socket, threading

class Counter:
    def __init__(self):
        self._value = 0
        self._LOCK = threading.Lock()

    def increment(self):
        with self._LOCK:
            self._value += 1
            return self._value

def pr(out):
    sys.stdout.write('{}\n'.format(out))
    sys.stdout.flush()

def recvAll(sock):
    data = ''
    BUF_SIZE = 1024

    while True:
        part = sock.recv(BUF_SIZE)
        data += part
        length = len(part)
        if length < BUF_SIZE: break

    return data

class Speed(threading.Thread):
    _COUNTER = Counter()

    def __init__(self):
        super(Speed, self).__init__()
        self.daemon = True
        self._sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
        self._sock.settimeout(5)
        self._sock.connect(('checkip.amazonaws.com', 443))
        self._request = 'GET / HTTP/1.1\r\n'
        self._request += 'Host: checkip.amazonaws.com\r\n\r\n'

    def run(self):
        i = 0
        while i < 5:
            self._sock.write(self._request)
            response = recvAll(self._sock)
            if '200 OK' not in response: continue
            count = Speed._COUNTER.increment()
            pr('#{} - {}'.format(count, response))
            i += 1

        self._sock.close()

def speed():
    sock = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM))
    sock.settimeout(5)
    sock.connect(('checkip.amazonaws.com', 443))
    request = 'GET / HTTP/1.1\r\n'
    request += 'Host: checkip.amazonaws.com\r\n\r\n'

    i = 0
    while i < 5:
        sock.write(request)
        response = recvAll(sock)
        if '200 OK' not in response: continue
        count = counter.increment()
        pr('#{} - {}'.format(count, response))
        i += 1

    sock.close()

slow = False

if slow:
    for _ in xrange(200):
        thread = Speed()
        thread.start()

else:
    counter = Counter()
    for _ in xrange(200):
        thread = threading.Thread(target = speed)
        thread.daemon = True
        thread.start()

while threading.active_count() > 1: time.sleep(1)

我希望两者的速度都差不多。但是,子类threading.Thread的变体要慢得多。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您的Thread子类在__init__中完成了太多的工作,该工作不会在新线程中执行。结果,使用该子类的版本最终会在很大程度上顺序执行。