February 2019
Intermediate to advanced
672 pages
16h 50m
English
The current version of our ping test application works as intended, but we can improve its readability by refactoring the logic where we make web requests into a thread class. Consider the Chapter12/example4.py file, specifically the MyThread class:
# Chapter12/example4.pyimport threadingimport requestsclass MyThread(threading.Thread): def __init__(self, url): threading.Thread.__init__(self) self.url = url self.result = None def run(self): res = requests.get(self.url) self.result = f'{self.url}: {res.text}'
In this example, MyThread inherits from the threading.Thread class and contains two additional attributes: url and result. The url attribute holds the URL that the thread instance should process, and the response ...
Read now
Unlock full access