Python+threading模塊對單個接口進行并發(fā)測試
更新時間:2019年06月25日 11:12:51 作者:夢四十九劍
這篇文章主要為大家詳細介紹了Python+threading模塊對單個接口進行并發(fā)測試,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Python threading模塊對單個接口進行并發(fā)測試的具體代碼,供大家參考,具體內(nèi)容如下
本文知識點
通過在threading.Thread繼承類中重寫run()方法實現(xiàn)定制輸出結(jié)果
代碼如下
import requests import threading import sys, io # 解決console顯示亂碼的編碼問題 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') class Mythread(threading.Thread): """This class customizes the output thu overriding the run() method""" def __init__(self, obj): super(Mythread, self).__init__() self.obj = obj def run(self): ret = self.obj.test_search_tags_movie() print('result--%s:\n%s' % (self.getName(), ret)) class Douban(object): """A class containing interface test method of Douban object""" def __init__(self): self.host = 'movie.douban.com' self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0', 'Referer':'https://movie.douban.com/', } def get_response(self, url, data): resp = requests.post(url=url, data=data, headers=self.headers).content.decode('utf-8') return resp def test_search_tags_movie(self): method = 'search_tags' url = 'https://%s/j/%s' % (self.host, method) post_data = { 'type':'movie', 'source':'index' } resp = self.get_response(url=url, data=post_data) return resp if __name__ == '__main__': douban = Douban() thds = [] for i in range(9): thd = Mythread(douban) thd.start() thds.append(thd) for thd in thds: thd.join()
運行結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 在Python中通過threading模塊定義和調(diào)用線程的方法
- python threading和multiprocessing模塊基本用法實例分析
- Python 多線程,threading模塊,創(chuàng)建子線程的兩種方式示例
- Python多線程模塊Threading用法示例小結(jié)
- Python線程threading模塊用法詳解
- Python threading模塊condition原理及運行流程詳解
- Python 多線程之threading 模塊的使用
- Python多線程編程之threading模塊詳解
- python threading模塊的使用指南
- Python常用模塊之threading和Thread模塊及線程通信
相關(guān)文章
Python基礎(chǔ)類繼承重寫實現(xiàn)原理解析
這篇文章主要介紹了Python基礎(chǔ)類繼承重寫實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04python實現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法
這篇文章主要介紹了python實現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法,涉及Python針對文件操作與編碼轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-08-08