python 多线程模拟多用户访问接口

# -- coding: utf-8 --**
import threading
import requests
from urllib.parse import quote


# 定义访问接口的函数
def access_api(user_id):
params='%E4%BB%80%E4%B9%88%E6%98%AF%E5%A2%9E%E5%80%BC%E7%A8%8E'
chinese_text = "什么是增值税"
url_encoded_text = quote(chinese_text)
url = "http://42.159.193.133:8886/question/"+url_encoded_text
response = requests.get(url)
print(f"User {user_id} - Response: {response.text}")

# 创建多个线程来模拟多个用户访问接口
num_users = 5
threads = []
import time

# 记录开始时间
start_time = time.time()



for i in range(num_users):
thread = threading.Thread(target=access_api, args=(i,))
threads.append(thread)
thread.start()

# 等待所有线程完成
for thread in threads:
thread.join()
# 在这里执行你的代码
# ...

# 记录结束时间
end_time = time.time()

# 计算耗时
elapsed_time = end_time - start_time
print(f"程序耗时:{elapsed_time:.6f} 秒")
print("All users have finished accessing the API.")

### Java中解决Thumbnailator导致的UnsupportedFormatException异常问题 当使用Thumbnailator库处理某些特定图像格式(如WebP)时,可能会遇到`net.coobird.thumbnailator.tasks.UnsupportedFormatException: No suitable ImageReader found for source data.` 这种异常表明Java的ImageIO API无法识别该图像格式[^1]。 #### 原因分析 此问题的根本原因是Java标准库中的ImageIO模块不支持某些特殊格式的图像文件(例如WebP)。因此,在尝试加载这些图像时,会因为缺少合适的`ImageReader`而引发异常[^2]。 #### 解决方案 为了使Thumbnailator能够正常处理此类图像格式,可以引入第三方库来扩展ImageIO的功能。以下是具体解决方案: 1. **引入TwelveMonkeys库** TwelveMonkeys是一个强大的图像处理库,它提供了对多种图像格式的支持,包括WebP、TIFF等。通过添加以下Maven依赖项,可以使ImageIO支持更多图像格式: ```xml <dependency> <groupId>com.twelvemonkeys.imageio</groupId> <artifactId>imageio-webp</artifactId> <version>3.7.0</version> </dependency> ``` 添加上述依赖后,重新构建项目并运行代码即可解决问题[^4]。 2. **验证图像路径有效性** 确保输入的图像路径正确无误,并且目标文件确实存在。如果路径有误或者文件损坏,也可能触发类似的异常[^3]。 3. **更新Thumbnailator版本** 使用最新版的Thumbnailator可能有助于减少兼容性问题。可以通过检查官方文档或GitHub仓库获取最新的稳定发布版本号。 #### 示例代码调整 基于以上修改后的完整示例代码如下所示: ```java import net.coobird.thumbnailator.Thumbnails; public class ThumbnailExample { public static void main(String[] args) { String inputPath = "/Users/xxxx/Downloads/ysdq/formatImage/xxx.webp"; String outputPath = "/Users/xxxx/Downloads/ysdq/output/webp_thumbnail.jpg"; try { Thumbnails.of(inputPath) .size(900, 900) .outputFormat("jpg") // 可选:转换为目标格式 .toFile(outputPath); } catch (Exception e) { e.printStackTrace(); } } } ``` 注意:这里假设已经成功集成了`imageio-webp`插件以支持WebP格式。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值