从pydantic对象列表中创建JSON字符串列表
我正在尝试从pydantic对象列表中获取JSON字符串列表,并以下面的方式完成:
raw_books = [json.dumps(x.dict()) for x in batch.books]
一切正常,但是当列表包含数千个元素时,似乎需要很多时间。更有效地进行这种转变的另一种方法是什么?
例如: batch.books中的2000个列表元素导致执行时间约为5秒。
Pydantic模型:
class Book(BaseModel):
title: constr(min_length=1, max_length=128)
authors: List[constr(min_length=1, max_length=128)]
price: float
year_of_publishing: int
publisher: constr(min_length=1, max_length=128)
class BulkBook(BaseModel):
books: List[Book]
I am trying to get a list of json strings from a list of Pydantic objects and have done it in the following way:
raw_books = [json.dumps(x.dict()) for x in batch.books]
Everything works fine, but it seems to take a lot of time when the list holds thousands of elements. What would be another way to do this transformation more efficiently?
For example: 2000 list elements in batch.books result in execution times of around 5 seconds.
The pydantic model(s):
class Book(BaseModel):
title: constr(min_length=1, max_length=128)
authors: List[constr(min_length=1, max_length=128)]
price: float
year_of_publishing: int
publisher: constr(min_length=1, max_length=128)
class BulkBook(BaseModel):
books: List[Book]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此我们谈论的是
5000/2000 每个
list
的MS,对吗?2.5
用于转换list
的MS(您在哪些元素上json.dumps(x.dict())
))不是这样我猜很长一段时间。So we are talking of about
5000/2000
ms for eachlist
, right?2.5
ms for the conversion of alist
(on which elements you are callingjson.dumps(x.dict())
) it's not such a long time I guess.您正在调用
json.dumps
<代码>列表理解,这似乎是一个问题。预计这确实会导致性能大幅下降,因为dumps
在循环中的每次迭代都调用。最简单的解决方案是将
json.dumps
移出list
完全理解:You are calling
json.dumps
inside thelist
comprehension, that seems to be a problem. That would be expected do cause a huge dip in performance, becausedumps
is called for each iteration in the loop.The simplest solution would be to move
json.dumps
out of thelist
comprehension entirely: