flask redis_在Flask应用程序中将Redis队列用于异步任务

flask redis

When building an application that performs time-consuming, complex, or resource-intensive tasks, it can be frustrating to wait for these to complete within the front end application. Additionally, complex tasks in the front end can time-out. Redis Queue fixes this by pushing more sophisticated tasks to a worker for processing.

在构建执行耗时,复杂或资源密集型任务的应用程序时,等待这些任务在前端应用程序中完成可能会令人沮丧。 此外,前端的复杂任务可能会超时。 Redis Queue通过将更复杂的任务推送给工作人员进行处理来解决此问题。

Using Redis with Redis Queue allows you to enter those complex tasks into a queue, so the Redis worker executes these tasks outside of your application’s HTTP server.

通过将Redis与Redis Queue一起使用,您可以将那些复杂的任务输入队列,因此Redis worker在应用程序的HTTP服务器之外执行这些任务。

In this article, we will build an app that enqueues jobs with Redis queue, performs a function on those jobs and returns the result of the function.

在本文中,我们将构建一个应用程序,该应用程序使用Redis队列使作业排队,对这些作业执行功能,并返回该功能的结果。

Here is the link to the Github Repository with our code for this project.

这是Github信息库的链接,以及该项目的代码。

什么是Redis? (What is Redis?)

Redis is an open-source, in-memory database, cache, and message broker. Messages handled by Redis are essentially JSONs. Redis is ideal for quickly and easily working with specific data types in a temporary database, and provides very rapid access and delivery of queries.

Redis是一个开源的内存数据库,缓存和消息代理。 Redis处理的消息本质上是JSON。 Redis是快速,轻松地使用临时数据库中特定数据类型的理想选择,并且可以非常快速地访问和交付查询。

For us, Redis offers two benefits. First, it pushes complex tasks to another space to be processed. Second, it is easier for the developer to handle complex actions by splitting the task into separate functions, the main application and the queue.

对于我们来说,Redis提供了两个好处。 首先,它将复杂的任务推到另一个要处理的空间。 其次,通过将任务分为独立的功能,主应用程序和队列,开发人员可以更轻松地处理复杂的操作。

For this application, we’ll be using Redis to hold our queue of JSON messages. Redis can be a stand-alone database, accessible by many computers or systems. In our example, we will be using it as a local memory store to support our application.

对于此应用程序,我们将使用Redis来保存JSON消息队列。 Redis可以是一个独立的数据库,可以被许多计算机或系统访问。 在我们的示例中,我们将使用它作为本地内存存储来支持我们的应用程序。

什么是Redis Queue? (What is Redis Queue?)

Redis Queue is a python library for queueing jobs for background processing. Since many hosting services will time out on long HTTP requests, it is best to design APIs to close requests as quickly as possible. Redis Queue allows us to do this by pushing tasks to a queue and then to a worker for processing.

Redis Queue是一个用于对作业进行排队以进行后台处理的python库。 由于许多托管服务会在较长的HTTP请求上超时,因此最好设计API以尽快关闭请求。 Redis Queue允许我们通过将任务推入队列,然后推给工作人员进行处理来做到这一点。

Using Redis in conjunction with Redis Queue allows you to request input from the user, return a validation response to the user, and queue up processes in the background. All without the front end user having to wait for those processes to complete. Processes could be anything from Machine Learning models, to duplicating an image to complex simulations.

将Redis与Redis Queue结合使用可以使您从用户请求输入,向用户返回验证响应,并在后台排队进程。 所有这些都无需前端用户等待这些过程完成。 从机器学习模型到将图像复制到复杂的模拟,过

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值