五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

Python import模塊的緩存問題解決方案

發(fā)布日期:2022-03-24 19:27 | 文章來源:源碼中國

在使用django開發(fā)的平臺中,支持用戶自定義函數(shù),但是每次用戶進行修改編輯后,該模塊內(nèi)容已更改,然后重新導(dǎo)入該模塊,但是Python 會認(rèn)為“我已經(jīng)導(dǎo)入了該模塊,不需要再次讀取該文件”,所以更改將無效。

因此,每次更改文件的內(nèi)容時,都必須退出并重新啟動Django。

使用python開發(fā)后臺服務(wù)程序的時候,每次修改代碼之后都需要重啟服務(wù)才能生效比較麻煩

要解決這個問題,有以下幾種方式:

最簡單、最有效的方法:重新啟動 Django。但是,這也有缺點,特別是丟失了 django名稱空間中存在的數(shù)據(jù)以及其他導(dǎo)入模塊中的數(shù)據(jù)。

對于簡單的情況,可以使用 Python 的​reload()​函數(shù)。在許多情況下,在編輯一個模塊之后使用

​reload()​函數(shù)就足夠滿足需求。

這里主要是介紹第二種方式:

​reload()​是 Python 提供的內(nèi)置函數(shù),在不同的 Python 版本中有不同的表現(xiàn)形式:

在 Python 2.x 中,reload()是內(nèi)置函數(shù)。

在 Python 3.0 - 3.3 中,可以使用imp.reload(module)。

在 Python 3.4 中,imp 已經(jīng)被廢棄,取而代之的是importlib。

Python2.7可以直接用reload():

python2 內(nèi)置函數(shù)reload(module)

Python3可以用下面幾種方法:

方法一:基本方法

from imp import reload
reload(module)

方法二:

import imp
imp.reload(module)

方法三:

import importlib
importlib.reload(module)

方法四:

from importlib import reload
reload(module)

說明:

module 必須是已經(jīng)成功導(dǎo)入的模塊

模塊被加載到內(nèi)存以后,更改文件內(nèi)容,已經(jīng)運行的程序不會生效的,可通過reload重新加載。

導(dǎo)入是一個開銷很大的操作。

python中緩存模塊的一些用法

一.問題描述

有時候可能需要緩存一些 成員方法的值, 可能成員方法的計算比較耗時,有時候不希望重復(fù)調(diào)用計算該值, 這個時候就可以緩存該值.

查了一下標(biāo)準(zhǔn)庫 有 functools.lru_cache 有一個 lru_cache 可以緩存成員函數(shù)的值,

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author: Frank 
@contact: frank.chang@shoufuyou.com
@file: test_lru_cache.py
@time: 2018/9/8 下午8:55
"""
import time
from functools import lru_cache
class Model:
 @lru_cache(maxsize=10)
 def calculate(self, number):
  print(f'calculate({number}) is  running,', end=' ')
  print('sleep  3s  ')
  time.sleep(3)
  return number * 3
if __name__ == '__main__':
 model = Model()
 for i in range(5):
  print(model.calculate(i))
 for i in range(5):
  print(model.calculate(i))

結(jié)果如下:

calculate(0) is running, sleep 3s
0
calculate(1) is running, sleep 3s
3
calculate(2) is running, sleep 3s
6
calculate(3) is running, sleep 3s
9
calculate(4) is running, sleep 3s
12
0
3
6
9
12

從結(jié)果開出來, 第二次計算的時候 , 就沒有計算 而是通過緩存取值, 所以成員方法只計算了一次.

lru_cache 可以指定 max_size 緩存的大小, typed bool 如果為True, 代表不同類型分別緩存. 如果達到max_size 淘汰策略是LRU, LRU是Least Recently Used的縮寫,即最近最少使用,常用于頁面置換算法.

二 第三方的模塊

第三方的模塊cachetools 已經(jīng)提供了很多緩存策略,直接拿來用一下.

來看下面的例子.

1 來看一個緩存成員方法例子

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author: Frank 
@contact: frank.chang@shoufuyou.com
@file: test_cache.py
@time: 2018/9/8 下午12:59
pip install cachetools
https://docs.python.org/3/library/operator.html
緩存成員方法的值 
cachetools  已經(jīng)實現(xiàn)好了, 直接用就可以了. 
"""
from operator import attrgetter
import time
from cachetools import LRUCache,RRCache , cachedmethod
class Model:
 def __init__(self, cachesize):
  self.cache = LRUCache(maxsize=cachesize)
 @cachedmethod(attrgetter('cache'))
 def get_double_num(self, num):
  """ return  2* num"""
  print(f'get_double_num({num})  is running')
  time.sleep(2)
  return num * 2
model = Model(cachesize=10)
print(model.get_double_num(10))
print(model.get_double_num(10))
print(model.get_double_num(10))
print(model.get_double_num(10))
print(model.get_double_num(10))
print(model.get_double_num(10))

結(jié)果如下:

get_double_num(10) is running
20
20
20
20
20
20
Process finished with exit code 0

可以看出, 值計算一次 函數(shù),第二次走的是緩存. 非常好用. 在初始化方法里面構(gòu)造一個緩存對象, 之后用 cachedmethod 修飾成員函數(shù),同時 用attrgetter(‘cache') 把cache 拿到就可以用了.

實際上 cachetools 實現(xiàn)了很多緩存策略,具體緩存策略可以參考下面的鏈接.

'Cache', 'LFUCache',
'LRUCache',
'RRCache', 'TTLCache',

‘cached', ‘cachedmethod' 這兩個分別用來修飾 函數(shù)和成員方法的.

2 來看一個 緩存函數(shù)

# 緩存 函數(shù)的值
from cachetools import cached
@cached(cache={})
def fib(n):
 print((f'fib({n}) is  running.'))
 return n if n < 2 else fib(n - 1) + fib(n - 2)
for i in range(20):
 print('fib(%d) = %d' % (i, fib(i)))
@cached(cache={})
def fun(n):
 print(f'fun({n}) is runnnig.')
 time.sleep(3)
 return n ** 2
if __name__ == '__main__':
 for _ in range(5):
  print(fun(4))

如果cache = None , 表示不緩存,該計算結(jié)果.

結(jié)果如下:

fun(4) is runnnig.
16
16
16
16
16

直接導(dǎo)入 cached 里面 傳入一個字典就可以了,用起來也比較方便.

實現(xiàn)分析:

緩存思路大致是一樣的, 首先先把參數(shù)hash 一下生成一個key, 然后看key 是否在自己的緩存里,不在就計算方法(函數(shù)),之后把key和對應(yīng)value 放到自己的子弟那里面. 如果下一次計算該值,生成一個key 看是否在 自己的字典里面,如果在直接返回即可. 當(dāng)然這是基本的思路, 里面還有用到 緩存淘汰策略, 多線程是否要加鎖,等比較復(fù)雜的問題.

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持本站。

海外服務(wù)器租用

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部