简体   繁体   English

SQLAlchemy:发送响应后写入数据库

[英]SQLAlchemy: writing to database after the response has been sent

I have a simple service that does approximately the following: 我有一个简单的服务,它大致执行以下操作:

  1. An HTTP client connects to the server HTTP客户端连接到服务器
  2. The server writes the sessionID of the client and the timestamp to the database, and in most cases just returns an empty response 服务器将客户端的sessionID和时间戳写入数据库,并且在大多数情况下仅返回空响应

(The cases when it does do real work and return actual data are irrelevant to this question) (它确实进行实际工作并返回实际数据的情况与该问题无关)

In order to return this response as soon as possible, I'd like to write the info to memcache in the request handler's body (because memcache is fast), and to spawn a separate thread where another function using SQLAlchemy will write it to the persistent storage. 为了尽快返回此响应,我想将信息写入请求处理程序主体中的memcache中(因为memcache快速),并产生一个单独的线程,其中另一个使用SQLAlchemy的函数会将其写入持久性存储。 This way, I'll be able to return immediately after writing to memcache and spawning a thread, and the request handler will not have to wait until SQLAlchemy saves the info to the database. 这样,我将能够在写入内存缓存并生成线程后立即返回,并且请求处理程序将不必等到SQLAlchemy将信息保存到数据库中。

Does this make sense? 这有意义吗? If yes, how should I implement it? 如果是,我应该如何实施?

You could use something like the Celery distributed task queue to offload processing to other machines. 您可以使用诸如Celery分布式任务队列之类的东西将处理任务卸载到其他计算机上。 It does require setup of a separate infrastructure, but will allow for tasks to be handed off from web requests for processing in the background, while the HTTP repsonse to the request can be returned immediately. 它确实需要设置单独的基础结构,但是将允许将任务从Web请求中移交给后台进行处理,而对请求的HTTP响应可以立即返回。

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

相关问题 Sqlalchemy 引擎 - 仅在创建包含表的数据库后运行函数 - Sqlalchemy engine - run a function only after a database with tables has been created Flask SQLAlchemy 数据库删除已提交的内容 - Flask SQLAlchemy Database Dropping what has been commited 发送输入后立即删除输入行 - Delete the input line right after input has been sent 使用 SQLalchemy 写入 Python 中的 PostgreSQL 数据库 - Writing to PostgreSQL database in Python with SQLalchemy 在Twisted中中止请求后,将响应发送回资源 - Send response back to resource after request has been aborted in Twisted 使用sqlalchemy设置值后如何运行“设置”事件侦听器 - How to run a 'set' event-listener after the value has been set with sqlalchemy 从 sqlalchemy 插入数据后,如何在 MySQL 上创建 UUID 触发器? - how to create a UUID trigger on MySQL after data has been inserted from sqlalchemy? 在 Python/CGI 中发送 HTTP POST 请求后,如何避免浏览器刷新页面 - How to be able to avoid a browser refreshing a page, after a HTTP POST request has been sent in Python/CGI 将数据从表单写入SQLAlchemy数据库 - writing data from forms to a SQLAlchemy database 使用Flask-SQLAlchemy,成功生成迁移脚本后,每次升级失败时,我应该做什么? - Using Flask-SQLAlchemy, what shoud I do each time an upgrade fails after a migration script has been successfully generated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM