简体   繁体   English

如何在所有其他请求中使用在第一个请求中创建的sklearn泡菜对象

[英]How to use sklearn pickle object created in first request across all other requests

I have the following use case In my project- Python. 我的项目Python中有以下用例。 We have a prediction system in place for every user request through API (We are using IIS, need to look at flask capabilities). 我们为通过API的每个用户请求提供了一个预测系统(我们正在使用IIS,需要查看烧瓶的功能)。

The size of a pickle file is huge, like 700MB, I don't want to load it for every request (since it is consuming more RAM for every request). 泡菜文件的大小非常大,例如700MB,我不想为每个请求都加载它(因为每个请求都消耗更多的RAM)。

Can we load the pickle file into flask cache and use it for all the requests? 我们可以将泡菜文件加载到Flask缓存中并用于所有请求吗? (or) Can we share the data among all the requests, so that we don't need to load it again and again? (或)我们可以在所有请求中共享数据,这样就不必一次又一次地加载它了吗?

Please help with the sample implementation of sharing pickle data across all the requests 请帮助实现在所有请求中共享泡菜数据的示例实现

You can try using something like Flask-Cache , and do a function like this: 您可以尝试使用Flask-Cache之类的东西,并执行以下功能:

@cache.cached(timeout=None)
def get_pickle_object():
    return your_pickle_object

This way the pickle object will only be imported the first time this function is called and afterward it will be dumped from cache. 这样,仅在第一次调用此函数时才导入pickle对象,然后将其从缓存中转储。

You can also call this once during app initialization so that it is already loaded for the first request. 您还可以在应用初始化期间调用一次,这样它就已经为第一个请求加载了。

Then every time you need the object, use get_pickle_object() instead of the object itself. 然后,每次需要该对象时,请使用get_pickle_object()而不是对象本身。

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

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