简体   繁体   English

如何在REST API服务器中处理高吞吐量功能?

[英]How do you handle high throughput functions in the REST API server?

I am developing the rest API using python flask. 我正在使用python flask开发其余的API。 (Client is a mobile app) (客户端是移动应用程序)

However, important functions are a batch program that reads data from DB processes it, and then updates (or inserts) the data when a user requests POST method with user data 但是,重要的函数是批处理程序,它从数据库中读取数据处理它,然后在用户请求带有用户数据的POST方法时更新(或插入)数据

Considering a lot of Read, Write, and Computation 考虑了大量的读,写和计算

How do you develop it? 你是如何发展它的?

This is how I think. 这就是我的想法。

  1. Use procedures in DB 使用DB中的过程

  2. Create an external deployment program that is independent of API. 创建独立于API的外部部署程序。

  3. Create a separate batch server 创建单独的批处理服务器

  4. Just run it on the API server 只需在API服务器上运行它

I can not judge what is right with my knowledge. 我不能用我的知识判断什么是正确的。

And the important thing is that execution speed should not be slow. 重要的是执行速度不应该慢。

For the user to feel, they should look as though they are running on their own devices. 为了让用户感觉到,他们应该看起来好像是在自己的设备上运行。

I would like to ask you for advice on back-end development. 我想请教您关于后端开发的建议。

I would recommand considering asyncio . 我建议考虑asyncio This is pretty much the use-case you have - i/o is time-consuming, but doesn't require lots of CPU. 这几乎就是你的用例 - i / o非常耗时,但不需要大量的CPU。 So essentially you would want that i/o to be done asynchronously, while the rest of the server carries on. 所以基本上你会希望i / o异步完成,而服务器的其余部分继续。

  • The server receives some requests that requires i/o. 服务器接收一些需要i / o的请求。
  • It spins off that request into your asyncio architecture, so it can be performed. 它将该请求转换为您的asyncio架构,因此可以执行。
  • The server is already available to receive other requests, while the previous i/o requests is being processed. 在处理先前的i / o请求时,服务器已可用于接收其他请求。
  • The previous i/o requests finishes. 先前的i / o请求完成。 Asyncio offers a few ways to deal with this. Asyncio提供了几种方法来解决这个问题。

See the docs, but you could provide a callback, or build your logic to take advantage of Asyncio's event loop (which essentially manages switching back & forth between context, eg the "main" context of your server serving resquests and the async i/o operations that you have queued up). 查看文档,但您可以提供回调,或构建您的逻辑以利用Asyncio的事件循环 (它实质上管理在上下文之间来回切换,例如服务器服务请求的“主”上下文和异步i / o排队的操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM