首页 > 编程语言 >【python】多线程并发,rpc接口性能测试

【python】多线程并发,rpc接口性能测试

时间:2023-03-21 15:47:15浏览次数:49  
标签:concurrent python futures url rpc future https 多线程

1、官方文档

https://docs.python.org/3/library/concurrent.futures.html

 

2、安装

python 3.x中自带了concurrent.futures模块

python 2.7需要安装futures模块,使用命令pip install futures安装即可

pypi地址:https://pypi.python.org/pypi/futures/

 

3、ThreadPoolExecutor 示例

import concurrent.futures
import urllib.request

URLS = ['http://www.foxnews.com/',
        'http://www.cnn.com/',
        'http://europe.wsj.com/',
        'http://www.bbc.co.uk/',
        'http://nonexistant-subdomain.python.org/']

# Retrieve a single page and report the URL and contents
def load_url(url, timeout):
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()

# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    # Start the load operations and mark each future with its URL
    future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
    for future in concurrent.futures.as_completed(future_to_url):
        url = future_to_url[future]
        try:
            data = future.result()
        except Exception as exc:
            print('%r generated an exception: %s' % (url, exc))
        else:
            print('%r page is %d bytes' % (url, len(data)))

 

参考链接

https://docs.python.org/3/library/concurrent.futures.html

https://blog.csdn.net/xixihahalelehehe/article/details/107308083

https://www.cnblogs.com/CheeseZH/p/12713977.html

https://blog.csdn.net/weixin_41951954/article/details/122231732

标签:concurrent,python,futures,url,rpc,future,https,多线程
From: https://www.cnblogs.com/fireblackman/p/17240240.html

相关文章

  • 如何写好Python的Lambda函数?
    当你需要完成一件小工作时,在本地环境中使用这个函数,可以让工作如此得心应手,它就是Lambda函数。Lambda函数是Python中的匿名函数。有些人将它们简称为lambdas,它们的语......
  • 100道python基础题——(15)
    题:编写一个程序,计算a+aa+aaa+aaaa的值,给定的数字作为a的值。假设为程序提供了以下输入:9  然后,输出应该是:11106提示:如果输入数据被提供给问题,则应该假定它是控......
  • 100道python基础题——(14)
    题:编写一个接受句子的程序,并计算大写字母和小写字母的数量。假设为程序提供了以下输入:Helloworld!然后,输出应该是:大写实例1小写实例9提示:如果输入数据被提供给问题,则应......
  • Python装饰器【杭州多测师_王sir】
    defmy_decorator(param):defdecorator(func):defwrapper(*args,**kwargs):print(f"param:{param}")returnfunc(*args,**......
  • python hmac.new示例
      hmac是Python中的一个模块,它提供了一个类和一些函数,用于使用密钥对消息进行哈希处理。哈希处理是一种将任意长度的消息转换为固定长度哈希值的方法。哈希值通常用于......
  • Python中获取文件大小
    这篇文章将讨论如何在Python中获取文件的大小。1.使用os.stat()功能获取文件状态的标准解决方案是使用os.stat()Python函数。它返回一个stat_result对象,它有一......
  • python3中使用tf
    python3中importtf报错,记录,ubuntu18.04环境,树莓派。 1、注释掉.bashrc#source~/你的workspace/devel/setup.bash2、安装catkinbuildsudoapt-getinstallpython......
  • Python 单例实现
    Python 单例实现使用装饰器实现单例模式使用装饰器实现单例模式的方法比较简单,只需要定义一个装饰器函数,将其应用到需要实现单例模式的类上即可。具体的实现方法如下:de......
  • Python查看对象所占内存大小
    以下内容摘自ChatGPT在Python中,可以使用sys模块中的getsizeof()函数来查看一个数据结构所占用的内存大小。该函数返回对象占用的字节数,但是需要注意以下几点:1.getsizeo......
  • 对斗破苍穹进行python文本分析
    对斗破苍穹进行python文本分析用python分析该小说的分词,词频,词云,小说人物出场次数排序等等。1、分词对文本进行分词,将分词结果输出到文本文件中。自己创建一个txt文本......