私信  •  关注

imposeren

imposeren 最近创建的主题
imposeren 最近回复了
4 年前
回复了 imposeren 创建的主题 » 如何销毁Python对象并释放内存

pd.DataFrame(...) 可能会在某些linux版本上泄漏(请参阅github issue and "workaround" ),所以即使 del df 可能没用。

pd.DataFrame.__del__ :

from ctypes import cdll, CDLL
try:
    cdll.LoadLibrary("libc.so.6")
    libc = CDLL("libc.so.6")
    libc.malloc_trim(0)
except (OSError, AttributeError):
    libc = None


if no libc:
    print("Sorry, but pandas.DataFrame may leak over time even if it's instances are deleted...")


CHUNK_SIZE = 20000


#file_list_1 contains 100,000 images
with ThreadPool(64) as pool:
    for count,f in enumerate(divide_chunks(file_list_1, CHUNK_SIZE)):
        # make the Pool of workers
        results = pool.map(get_image_features,f)
        # close the pool and wait for the work to finish 
        list_a, list_b = zip(*results)
        df = pd.DataFrame({'filename':list_a,'image_features':list_b})
        df.to_pickle("PATH_TO_FILE"+str(count)+".pickle")

        del df

        # 2 new lines of code:
        if libc:  # Fix leaking of pd.DataFrame(...)
            libc.malloc_trim(0)

print("pool closed")

P、 如果任何一个数据帧太大,这个解决方案将没有帮助。只有通过减少 CHUNK_SIZE

4 年前
回复了 imposeren 创建的主题 » 确定django模型实例更新了多少次

没有通用的方法可以得到这个。如所述 wim 您可以使用一些“版本控制包”来跟踪更改的整个历史记录。我个人也曾使用过同样的建议:Django回复,但是 other alternatives 是的。

如果只需要跟踪某些字段,则可以自己编程一些更简单的机制:

  1. 创建模型/字段以跟踪您的信息
  2. 使用类似于 FieldTracker 跟踪对特定字段的更改
  3. 创建处理程序保存后信号(或只是修改模型的 save 方法)保存数据

您也可以使用类似“表审计”的东西。我自己也没试过,不过也有一些套餐:

4 年前
回复了 imposeren 创建的主题 » 如何使for循环在python中更容易理解?

不是“多”或“少”可以理解,而是你如何描述它:

  • for x in something :迭代来自iterable的值 something 作为变量 x . 所以你需要解释的是 range(...) “。
  • range(start, end,...) 返回从开始(包含)到停止(排除)生成整数序列的对象。给他们看看 help(range)

你问的是“如何使for循环更像c?”

有一些方法可以做到这一点:


def nrange(start, num_elements, step=1):
    """Similar to `range`, but second argument is number of elements."""
    return range(start, start + step*num_elements, step)


def inclusive_range(start_or_stop, stop=None, step=1):
    if stop is None:
        start = 0
        stop = start_or_stop+1
    else:
        start = start_or_stop
        stop = stop + step
    return range(start, stop, step)


_default_update_func = lambda item: item+1
def c_like_iterator(start, test_func, update_func=_default_update_func):
    cur_value = start
    while test_func(cur_value):
        yield cur_value
        cur_value = update_func(cur_value)

for i in nrange(1, 10):
    print(i)

for i in inclusive_range(1, 10):
    print(i)

for i in inclusive_range(10):  # this will give 11 elements because both 0 and 10 values are included
    print(i)

for i in c_like_iterator(1, lambda x: x<=10, lambda x: x+1):
    print(i)

for i in c_like_iterator(1, lambda x: x<11, lambda x: x+1):
    print(i)

for i in inclusive_range(1, -10, -1):
    print(i)