python-3.x - 减少对'_thread.lock'对象的{方法'acquire'}python的调用次数
问题描述
嗨,我正在努力使用我的 I/O 绑定应用程序,以使其对潜在用户来说足够快
我正在获取 X 个 URL,例如 10 个,使用 MULTI THREADING ,每个 URL 有 1 个线程
但这花费了太长时间,我在我的代码上运行了 Cprofile,我发现瓶颈在于 “{method 'acquire' of '_thread.lock' objects}”
在 Cprofile 结果中,我注意到方法“获取”被称为每个线程 9 次
任何人都可以阐明我如何减少每个线程的调用次数 这是一个示例代码:
url_to_get = ["https://api.myip.com","https://api.myip.com","https://api.myip.com","https://api.myip.com",
"https://api.myip.com","https://api.myip.com","https://api.myip.com","https://api.myip.com",
"https://api.myip.com","https://api.myip.com"]
def fetch(url):
with requests.get(url,proxies=proxy) as response:
print(response.text)
def main():
with ThreadPoolExecutor(max_workers=10) as executor:
executor.map(fetch, url_to_get)
if __name__ == '__main__':
import cProfile, pstats
profiler = cProfile.Profile()
profiler.enable()
main()
profiler.disable()
stats = pstats.Stats(profiler).sort_stats('tottime')
stats.print_stats(10)
Cprofile 结果:
ncalls tottime percall cumtime percall filename:lineno(function)
90 3.581 0.040 3.581 0.040 {method 'acquire' of '_thread.lock' objects}
10 0.001 0.000 0.001 0.000 C:\Users\MINOUSAT\AppData\Local\Programs\Python\Python38-32\lib\threading.py:1177(_make_invoke_excepthook)
10 0.001 0.000 0.001 0.000 {built-in method _thread.start_new_thread}
10 0.000 0.000 0.028 0.003 C:\Users\MINOUSAT\AppData\Local\Programs\Python\Python38-32\lib\concurrent\futures\thread.py:193(_adjust_thread_count)
20 0.000 0.000 0.025 0.001 C:\Users\MINOUSAT\AppData\Local\Programs\Python\Python38-32\lib\threading.py:270(wait)
21 0.000 0.000 0.000 0.000 C:\Users\MINOUSAT\AppData\Local\Programs\Python\Python38-32\lib\threading.py:222(__init__)
10 0.000 0.000 0.028 0.003 C:\Users\MINOUSAT\AppData\Local\Programs\Python\Python38-32\lib\concurrent\futures\thread.py:158(submit)
32 0.000 0.000 0.000 0.000 {built-in method _thread.allocate_lock}
10 0.000 0.000 0.001 0.000 C:\Users\MINOUSAT\AppData\Local\Programs\Python\Python38-32\lib\threading.py:761(__init__)
10 0.000 0.000 0.025 0.002 C:\Users\MINOUSAT\AppData\Local\Programs\Python\Python38-32\lib\threading.py:540(wait)
太感谢了
解决方案
推荐阅读
- java - 如何控制相机在 Android 中看到的内容
- php - PHP Laravel 雄辩与多个数据库,实际上选择了一些东西
- c# - 为什么我的 lambda 查询在选择单列时抛出错误?
- python-3.x - 在过滤数据框时返回整个数据框(Python / Pandas)
- c# - 为什么不等待我传递给 Task.Run() 的异步操作?
- mysql - 如果使用左连接找不到记录,如何将记录计数为零?
- python - Python mysql复制客户端不显示值
- sql - 过滤掉,以便每个唯一 ID 仅显示最新日期
- regex - HTTP 查询参数正则表达式
- multithreading - 如何创建触发器以并行运行多个函数(传递参数)