Python 3:对线程类进行子类化?

时间:2017-11-24 20:31:50

标签: python multithreading python-3.x inheritance

我正在尝试将线程类子类化为在调用URL并使用以下代码获取响应时生成线程:

import urllib
import threading

from urllib.request import urlopen

class DocConnection(threading.Thread):
    def _init_(self,query, timeout):
        self.query = query
        self.timeout = timeout
        threading.Thread.__init__(self)
    def run(self):
        try:
            data = urllib.urlencode({'docQuery': self.query})
            request = urllib.request.Request('/my/url', data)
            http_file = urlopen(request, timeout=self.timeout)
            self.result = http_file.read()
            return
        except (urllib.HTTPError) as e:
            err = 'HTTP Error'
        except (urllib.URLError) as e:
            err = 'URL Error'

        print(err)
        #sublime.error_message(err)
        return False

e = DocConnection("peprika",12)
e.start()

但是,我收到以下错误:

Traceback (most recent call last):
  File "C:/Users/peprika/PycharmProjects/DocSublimePlugin/Documentation.py", line 28, in <module>
    e = DocConnection("peprika",12)
  File "C:\Users\peprika\AppData\Local\Programs\Python\Python35-32\lib\threading.py", line 778, in __init__
    **assert group is None, "group argument must be None for now"
AssertionError: group argument must be None for now**

有人可以帮我理解这个问题吗?

1 个答案:

答案 0 :(得分:0)

你拼错了init函数。它是__init__而不是_init_。在这种情况下,当您使用这两个属性实例化继承的类时,它将使用这些属性调用Thread构造函数。根据文档,其中一个是group,应该是None。