Python+threading模塊對(duì)單個(gè)接口進(jìn)行并發(fā)測(cè)試
本文實(shí)例為大家分享了Python threading模塊對(duì)單個(gè)接口進(jìn)行并發(fā)測(cè)試的具體代碼,供大家參考,具體內(nèi)容如下
本文知識(shí)點(diǎn)
通過(guò)在threading.Thread繼承類中重寫(xiě)run()方法實(shí)現(xiàn)定制輸出結(jié)果
代碼如下
import requests import threading import sys, io # 解決console顯示亂碼的編碼問(wèn)題 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()
運(yùn)行結(jié)果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 在Python中通過(guò)threading模塊定義和調(diào)用線程的方法
- python threading和multiprocessing模塊基本用法實(shí)例分析
- Python 多線程,threading模塊,創(chuàng)建子線程的兩種方式示例
- Python多線程模塊Threading用法示例小結(jié)
- Python線程threading模塊用法詳解
- Python threading模塊condition原理及運(yùn)行流程詳解
- Python 多線程之threading 模塊的使用
- Python多線程編程之threading模塊詳解
- python threading模塊的使用指南
- Python常用模塊之threading和Thread模塊及線程通信
相關(guān)文章
使用python的pandas為你的股票繪制趨勢(shì)圖
這篇文章主要介紹了通過(guò)python為你的股票繪制趨勢(shì)圖,動(dòng)手寫(xiě)個(gè)小程序, 把股票趨勢(shì)每天早上發(fā)到郵箱里,用 python 的 pandas, matplotlib 寫(xiě)起來(lái)很容易, 幾十行代碼搞定。,需要的朋友可以參考下2019-06-06Python基礎(chǔ)類繼承重寫(xiě)實(shí)現(xiàn)原理解析
這篇文章主要介紹了Python基礎(chǔ)類繼承重寫(xiě)實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04python實(shí)現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法
這篇文章主要介紹了python實(shí)現(xiàn)JAVA源代碼從ANSI到UTF-8的批量轉(zhuǎn)換方法,涉及Python針對(duì)文件操作與編碼轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-08-08python實(shí)現(xiàn)對(duì)文件進(jìn)行MD5校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了如何使用python對(duì)文件進(jìn)行MD5校驗(yàn)并比對(duì)文件重復(fù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01django項(xiàng)目登錄中使用圖片驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要介紹了django項(xiàng)目登錄中使用圖片驗(yàn)證碼的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-08-08