[英]SQLAlchemy: writing to database after the response has been sent
I have a simple service that does approximately the following: 我有一个简单的服务,它大致执行以下操作:
(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.