gpt4 book ai didi

python - peewee 和 peewee 异步 : why is async slower

转载 作者:太空狗 更新时间:2023-10-29 21:28:14 27 4
gpt4 key购买 nike

我正在努力思考 Tornado 和与 Postgresql 的异步连接。我在 http://peewee-async.readthedocs.io/en/latest/ 找到了可以执行此操作的库.

我设计了一个小测试来比较传统的 Peewee 和 Peewee 异步,但不知何故异步工作速度较慢。

这是我的应用:

import peewee
import tornado.web
import logging
import asyncio
import peewee_async
import tornado.gen
import tornado.httpclient
from tornado.platform.asyncio import AsyncIOMainLoop

AsyncIOMainLoop().install()
app = tornado.web.Application(debug=True)
app.listen(port=8888)

# ===========
# Defining Async model
async_db = peewee_async.PooledPostgresqlDatabase(
'reminderbot',
user='reminderbot',
password='reminderbot',
host='localhost'
)
app.objects = peewee_async.Manager(async_db)
class AsyncHuman(peewee.Model):
first_name = peewee.CharField()
messenger_id = peewee.CharField()
class Meta:
database = async_db
db_table = 'chats_human'


# ==========
# Defining Sync model
sync_db = peewee.PostgresqlDatabase(
'reminderbot',
user='reminderbot',
password='reminderbot',
host='localhost'
)
class SyncHuman(peewee.Model):
first_name = peewee.CharField()
messenger_id = peewee.CharField()
class Meta:
database = sync_db
db_table = 'chats_human'

# defining two handlers - async and sync
class AsyncHandler(tornado.web.RequestHandler):

async def get(self):
"""
An asynchronous way to create an object and return its ID
"""
obj = await self.application.objects.create(
AsyncHuman, messenger_id='12345')
self.write(
{'id': obj.id,
'messenger_id': obj.messenger_id}
)


class SyncHandler(tornado.web.RequestHandler):

def get(self):
"""
An traditional synchronous way
"""
obj = SyncHuman.create(messenger_id='12345')
self.write({
'id': obj.id,
'messenger_id': obj.messenger_id
})


app.add_handlers('', [
(r"/receive_async", AsyncHandler),
(r"/receive_sync", SyncHandler),
])

# Run loop
loop = asyncio.get_event_loop()
try:
loop.run_forever()
except KeyboardInterrupt:
print(" server stopped")

这是我从 Apache Benchmark 得到的结果:

ab -n 100 -c 100 http://127.0.0.1:8888/receive_async

Connection Times (ms)
min mean[+/-sd] median max
Connect: 2 4 1.5 5 7
Processing: 621 1049 256.6 1054 1486
Waiting: 621 1048 256.6 1053 1485
Total: 628 1053 255.3 1058 1492

Percentage of the requests served within a certain time (ms)
50% 1058
66% 1196
75% 1274
80% 1324
90% 1409
95% 1452
98% 1485
99% 1492
100% 1492 (longest request)




ab -n 100 -c 100 http://127.0.0.1:8888/receive_sync
Connection Times (ms)
min mean[+/-sd] median max
Connect: 2 5 1.9 5 8
Processing: 8 476 277.7 479 1052
Waiting: 7 476 277.7 478 1052
Total: 15 481 276.2 483 1060

Percentage of the requests served within a certain time (ms)
50% 483
66% 629
75% 714
80% 759
90% 853
95% 899
98% 1051
99% 1060
100% 1060 (longest request)

为什么同步更快?我缺少的瓶颈在哪里?

最佳答案

详细解释:

http://techspot.zzzeek.org/2015/02/15/asynchronous-python-and-databases/

简短说明:同步Python代码简单,主要在标准库的套接字模块中实现,它是纯C语言。异步Python代码比同步代码更复杂。每个请求都需要多次执行主事件循环代码,该代码是用 Python 编写的(在此处的 asyncio 案例中),因此与 C 代码相比有很多开销。

像您这样的基准测试显着地显示了异步的开销,因为您的应用程序和数据库之间没有网络延迟,而且您正在执行大量非常小的数据库操作。由于基准测试的每个其他方面都很快,因此事件循环逻辑的这么多次执行增加了总运行时间的很大一部分。

上面链接的 Mike Bayer 的论点是,像这样的低延迟场景对于数据库应用程序来说是典型的,因此数据库操作不应该在事件循环中运行。

异步最适合高延迟场景,例如 websocket 和网络爬虫,在这些场景中,应用程序将大部分时间用于等待对等点,而不是将大部分时间用于执行 Python。

总而言之:如果您的应用程序有充分的理由采用异步(它处理速度较慢的对等点),那么为了保持代码的一致性,使用异步数据库驱动程序是个好主意,但需要一些开销。

如果您出于其他原因不需要异步,请不要执行异步数据库调用,因为它们有点慢。

关于python - peewee 和 peewee 异步 : why is async slower,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39803746/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com