huge-success/sanic

Sanic:快速Python Web框架
Sanic是一款专为Python 3.6+设计的高性能Web服务器和框架,利用async/await语法实现非阻塞操作,提升应用速度。支持ASGI规范,便于部署。本文介绍Sanic的基本使用方法及安装步骤。

Sanic | Build fast. Run fast.
Build
Build Status AppVeyor Build Status Codecov
Docs Documentation
Package
PyPI PyPI version PyPI Wheel Supported implementations Code style black
Support
Forums Join the chat at https://gitter.im/sanic-python/Lobby Awesome Sanic List
Stats
Downloads Downloads
Sanic is a Python 3.6+ web server and web framework that’s written to go fast. It allows the usage of the async/await syntax added in Python 3.5, which makes your code non-blocking and speedy.

Sanic is also ASGI compliant, so you can deploy it with an alternative ASGI webserver.

Source code on GitHub | Help and discussion board.

The project is maintained by the community, for the community. Contributions are welcome!

The goal of the project is to provide a simple way to get up and running a highly performant HTTP server that is easy to build, to expand, and ultimately to scale.

Installation
pip3 install sanic

Sanic makes use of uvloop and ujson to help with performance. If you do not want to use those packages, simply add an environmental variable SANIC_NO_UVLOOP=true or SANIC_NO_UJSON=true at install time.

$ export SANIC_NO_UVLOOP=true
$ export SANIC_NO_UJSON=true
$ pip3 install --no-binary :all: sanic
Note

If you are running on a clean install of Fedora 28 or above, please make sure you have the redhat-rpm-config package installed in case if you want to use sanic with ujson dependency.

Note

Windows support is currently “experimental” and on a best-effort basis. Multiple workers are also not currently supported on Windows (see Issue #1517), but setting workers=1 should launch the server successfully.

Hello World Example
from sanic import Sanic
from sanic.response import json

app = Sanic()

@app.route(’/’)
async def test(request):
return json({‘hello’: ‘world’})

if name == ‘main’:
app.run(host=‘0.0.0.0’, port=8000)
Sanic can now be easily run using sanic hello.app.

[2018-12-30 11:37:41 +0200] [13564] [INFO] Goin’ Fast @ http://0.0.0.0:8000
[2018-12-30 11:37:41 +0200] [13564] [INFO] Starting worker [13564]
And, we can verify it is working: curl localhost:8000 -i

HTTP/1.1 200 OK
Connection: keep-alive
Keep-Alive: 5
Content-Length: 17
Content-Type: application/json

{“hello”:“world”}
Now, let’s go build something fast!

Documentation
Documentation on Readthedocs.

Changelog
Release Changelogs.

Questions and Discussion
Ask a question or join the conversation.

Contribution
We are always happy to have new contributions. We have marked issues good for anyone looking to get started, and welcome questions on the forums. Please take a look at our Contribution guidelines.

DPDK(Data Plane Development Kit)配置大页内存的代码主要作用是为数据包处理提供高性能的内存管理机制。大页内存(Huge Pages)是Linux内核提供的一种机制,通过使用更大的内存页(通常为2MB或1GB),减少页表项的数量,降低TLB(Translation Lookaside Buffer)的缺失率,从而提升内存访问效率。在DPDK中,大页内存的配置和管理是其高性能网络数据包处理的关键部分之一。 在DPDK中,配置大页内存的代码通常涉及初始化和管理大页内存的分配与映射。以下是对DPDK配置大页内存代码的核心作用解析: ### 大页内存的初始化 DPDK在启动时会初始化大页内存,通过创建`nr_hugepages`个`struct hugepage_file`结构体数组来管理每个大页内存页。`struct hugepage_file`结构体记录了每个大页内存页的虚拟地址、物理地址、大小、NUMA节点ID、文件ID、所属内存段ID以及文件路径等信息。这些信息用于后续的内存管理和访问[^3]。 ```c struct hugepage_file { void *orig_va; /**< 虚拟地址,首次mmap()的地址 */ void *final_va; /**< 虚拟地址,第二次mmap()的地址 */ uint64_t physaddr; /**< 物理地址 */ size_t size; /**< 内存页大小 */ int socket_id; /**< NUMA节点ID */ int file_id; /**< 文件ID,对应HUGEFILE_FMT中的'%d' */ int memseg_id; /**< 所属内存段ID */ #ifdef RTE_EAL_SINGLE_FILE_SEGMENTS int repeated; /**< 页面大小重复的次数 */ #endif char filepath[MAX_HUGEPAGE_PATH]; /**< 文件路径,指向文件系统中的大页内存文件 */ }; ``` ### 大页内存的分配与映射 DPDK通过两次`mmap()`调用来完成大页内存的分配与映射。第一次`mmap()`调用用于获取大页内存的虚拟地址(`orig_va`),第二次`mmap()`调用则用于将物理地址(`physaddr`)映射到用户空间的虚拟地址空间(`final_va`)。这种方式确保了DPDK可以在用户空间直接访问物理内存,绕过了内核协议栈的开销,从而提高数据包处理的性能[^3]。 ### NUMA节点的支持 DPDK支持多NUMA节点的内存管理,通过`socket_id`字段记录每个大页内存页所属的NUMA节点。NUMA(Non-Uniform Memory Access)架构下,访问本地节点的内存速度要快于访问远程节点的内存。因此,DPDK通过将内存分配到不同的NUMA节点上,可以进一步优化性能。 ### 文件系统支持 大页内存的文件路径(`filepath`)指定了大页内存文件在文件系统中的位置。DPDK通过将大页内存文件映射到用户空间,实现了对物理内存的直接访问。这种机制避免了频繁的用户态与内核态之间的数据拷贝,提升了数据包处理的效率[^3]。 ### 性能优化 通过使用大页内存,DPDK减少了页表项的数量,降低了TLB的缺失率,从而减少了内存访问的延迟。此外,大页内存的使用还减少了内存碎片,提高了内存利用率,这对于高性能网络数据包处理至关重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值