Python+threading模塊對單個(gè)接口進(jìn)行并發(fā)測試
本文實(shí)例為大家分享了Python threading模塊對單個(gè)接口進(jìn)行并發(fā)測試的具體代碼,供大家參考,具體內(nèi)容如下
本文知識點(diǎn)
通過在threading.Thread繼承類中重寫run()方法實(shí)現(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()
運(yùn)行結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 在Python中通過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中優(yōu)雅處理JSON文件的方法實(shí)例
JSON是一種輕量級的數(shù)據(jù)交換格式,JSON采用完全獨(dú)立于語言的文本格式,但是也使用了類似于C語言家族的習(xí)慣,這篇文章主要給大家介紹了關(guān)于Python中優(yōu)雅處理JSON文件的相關(guān)資料,需要的朋友可以參考下2021-12-12
Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖的相關(guān)資料,好的圖表能幫助我們深化數(shù)據(jù)的記憶點(diǎn),文中通過圖文以及代碼示例將實(shí)現(xiàn)的方法介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Python繪圖實(shí)現(xiàn)臺風(fēng)路徑可視化代碼實(shí)例
這篇文章主要介紹了Python繪圖實(shí)現(xiàn)臺風(fēng)路徑可視化代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Python使用bar繪制堆積/帶誤差棒柱形圖的實(shí)現(xiàn)
本文先講解bar參數(shù)如何使用,然后分別演示堆積柱形圖和帶誤差柱形圖畫法。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
基于Python實(shí)現(xiàn)nc批量轉(zhuǎn)tif格式
做項(xiàng)目有時(shí)會運(yùn)用到netCDF格式的氣象數(shù)據(jù),而ArcGIS中需要用柵格影像進(jìn)行處理,對于較多的文件,ArcGIS一個(gè)個(gè)手動轉(zhuǎn)換過于繁瑣,因此我們采用Python進(jìn)行轉(zhuǎn)換,下面就是Python實(shí)現(xiàn)nc批量轉(zhuǎn)tif格式的示例代碼,希望對你有所幫助2022-08-08
python自動定時(shí)任務(wù)schedule庫的使用方法
當(dāng)你需要在 Python 中定期執(zhí)行任務(wù)時(shí),schedule 庫是一個(gè)非常實(shí)用的工具,它可以幫助你自動化定時(shí)任務(wù),本文給大家介紹了python自動定時(shí)任務(wù)schedule庫的使用方法,需要的朋友可以參考下2024-02-02
Python如何實(shí)現(xiàn)PDF隱私信息檢測
隨著越來越多的個(gè)人信息以電子形式存儲和傳輸,確保這些信息的安全至關(guān)重要,本文將介紹如何使用Python檢測PDF文件中的隱私信息,需要的可以參考下2025-02-02

