6

I do not have much experience with Python and need to figure out a few things regarding buffering:

I want to generate a list in which I can allocate a certain amount of "buffer" space (do not know the specifics on this), to generate a list of integers. If the 'buffer' gets full I'm assuming it needs a flush command? Or how would you clear the buffer to continue putting things into that buffer?

Here is a sample of my code:

for i in range(0,500):
    randoms = random.randint(0,100000)
    looplist.append(randoms)

What I would want is in looplist, to be a sort of buffer I assume? in which if the maximum buffer space in looplist gets full, it would need to clear out (is there a pause during that time? or what happens) the list to continue re-generating integers to that list/ buffer.

Part 2 question: Would like explanation as simple of how a buffer could work for python? or does memory management of python just disable need to allocate own buffers? (can we still do it if we want too?)

I will edit my question if it seems to broad scope, trying to be as descriptive as I know how.

4

3 回答 3

3

在 Python 中,您不必关心内存管理。您不需要保留“缓冲区”,只需分配您想要的变量并使用它们。

如果分配太多(对于您的 RAM),您可能会遇到MemoryErrors,表明您没有足够的内存。

不过,您可以使用 Queue 作为某种缓冲区,例如

import random
from Queue import Queue, Full

q = Queue(100)

for i in range(10):
    #fill queue
    while True:
        try:
            q.put_nowait(random.randint(1,10))
        except Full:
            break
    #take some values from queue
    print "Round", i,
    number_of_values_to_get = random.randint(0,20)
    print "getting %i values." % number_of_values_to_get
    for j in range(number_of_values_to_get):
        value = q.get()
        print "  got value", value

我创建了一个大小为 100 的队列,即其中最多可以存储 100 个条目。如果你试图放入一个完整的队列,它会引发相应的异常,所以你最好抓住它。(这仅在您使用put_nowaitput超时时为真,请查看文档以获取更多详细信息。)

在这个例子中,十轮,我填充 100 个随机整数的“缓冲区”(队列)。然后,我从这个缓冲区中选择 0 到 20 个值并打印它们。

我希望这有帮助。队列的主要用例是多线程程序执行,它们是线程安全的。所以也许你会想从一个线程填充队列并从另一个线程读取它。如果您不想要多线程,collections.deque可能是一个更快的选择,但它们不是线程安全的。

于 2013-01-28T07:08:22.920 回答
1

每当队列/列表大小足够大时,使用队列/列表并使索引在先进先出的基础上下降

于 2013-01-28T06:35:00.223 回答
0

如果我理解正确,您是在问 Python 是否会为您管理列表的内存。答案是肯定的,您可以继续将项目附加到列表中,Python 会根据需要增加列表(当然,如果您无限期地继续追加,您将耗尽内存)。

我认为您也对限制这一点的方法感兴趣,因此您可以确保您的列表不会超出限制。如果是这种情况,您可能需要查看collections.deque对象。

例如:

import random
from collections import deque

def test_queue(max_size=10):
    d = deque([], max_size)
    for i in xrange(1, 2*max_size):
        r = random.randint(0, 100000)
        d.append(r)
        print 'i=%d, len=%d r=%d' % (i, len(d), r)

    while d:
        print d.popleft()

上述功能将确保deque您创建的对象不会超过max_size元素。如果您向 中添加更多元素deque,将自动删除最旧的元素。这是上述函数的示例运行:

i=1, len=1 r=83870
i=2, len=2 r=12432
i=3, len=3 r=87084
i=4, len=4 r=3485
i=5, len=5 r=12237
i=6, len=6 r=81401
i=7, len=7 r=24990
i=8, len=8 r=21391
i=9, len=9 r=34153
i=10, len=10 r=63651
i=11, len=10 r=96305
i=12, len=10 r=46671
i=13, len=10 r=19288
i=14, len=10 r=40170
i=15, len=10 r=45399
i=16, len=10 r=94032
i=17, len=10 r=57749
i=18, len=10 r=68440
i=19, len=10 r=59726
63651
96305
46671
19288
40170
45399
94032
57749
68440
59726
于 2013-01-28T07:09:29.120 回答