- Mastering Concurrency in Python
- Quan Nguyen
- 387字
- 2021-06-10 19:24:08
Refactoring request logic
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 Chapter05/example4.py file, specifically the MyThread class:
# Chapter05/example4.py
import threading
import requests
class 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 returned from the web server to that thread will be written to the result attribute (in the run() function).
Outside of this class, we now can simply loop through the URL list, and create and manage the threads accordingly while not having to worry about the request logic in the main program:
urls = [
'http://httpstat.us/200',
'http://httpstat.us/400',
'http://httpstat.us/404',
'http://httpstat.us/408',
'http://httpstat.us/500',
'http://httpstat.us/524'
]
start = time.time()
threads = [MyThread(url) for url in urls]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
for thread in threads:
print(thread.result)
print(f'Took {time.time() - start : .2f} seconds')
print('Done.')
Note that we are now storing the responses in the result attribute of the MyThread class, instead of directly printing them out as in the old ping() function from the previous examples. This means that, after making sure that all threads have finished, we will need to loop through the threads one more time and print out those responses.
Refactoring the request logic should not greatly affect the performance of our current program; we are keeping track of the execution speed to see if this is actually the case. Execute the program and you will obtain the output similar to the following:
http://httpstat.us/200: 200 OK
http://httpstat.us/400: 400 Bad Request
http://httpstat.us/404: 404 Not Found
http://httpstat.us/408: 408 Request Timeout
http://httpstat.us/500: 500 Internal Server Error
http://httpstat.us/524: 524 A timeout occurred
Took 0.14 seconds
Done.
Just as we expected, we are still achieving a significant speedup from the sequential version of the program with this refactored request logic. Again, our main program is now more readable, and further adjustments of the request logic (as we will see in the next section) can simply be directed to the MyThread class, without affecting the rest of the program.
- SQL Server 從入門到項目實踐(超值版)
- Java語言程序設計
- 深入核心的敏捷開發:ThoughtWorks五大關鍵實踐
- Mobile Web Performance Optimization
- Photoshop智能手機APP UI設計之道
- OpenShift在企業中的實踐:PaaS DevOps微服務(第2版)
- INSTANT Django 1.5 Application Development Starter
- 編寫高質量代碼:改善Objective-C程序的61個建議
- Android傳感器開發與智能設備案例實戰
- .NET Standard 2.0 Cookbook
- 零代碼實戰:企業級應用搭建與案例詳解
- Oracle實用教程
- 零基礎輕松學C++:青少年趣味編程(全彩版)
- Software Development on the SAP HANA Platform
- Python全棧開發:基礎入門