Python中使用Queue的Cache-mem工作



我已经编写了代码来模拟缓存的工作。

在这个模型中,我尝试实现fifo算法,它允许我们删除最后一个未使用的元素(数据,值,等等)。

我写了一个特殊的函数,它给了我一个列表o的数字(这些数字是内存中的地址)。
q=Queue.Queue(800)# Cache - memory. This is a queue which is more likely help me to simulate FIFO-algorithm
QW=[] # External memory
l=raw_input("Enter a desire operation:")#I enter my operation.
for i in range(len(o)):
    time.sleep(0.4)
    u = time.time()
    k=o.pop(0) #o - is the list with numbers (these numbers are addresses in memory). Here I get each address through pop.
    while l=='read': #If operation is "read" then i need to get my adress from q (cache-mem) or from QW (Is the external memory) and put it in q - (is the Cache-memory).
        if k not in q:
            if j in QW and k==j:
                q.put(j)
        else:
            q.get(k)
    while l=='record':#If operation is "record" then i need to write (append to QW) an address in QW or q, but only if the same address have existed already in QW or q.
        if k not in q:
            QW.append(k)
            print QW
        else:
            q.put(k)
    print q.get()

但是我得到错误:

TypeError: argument of type 'instance' is not iterable at line
  if k not in q

你不能这样做与队列我认为,而是使用collections.dequeue几乎相似,而集合有快速append(), pop()。您可以将代码的第一行更改为q=collections.deque([],800),并替换q.put,q. put。使用q.append, q.popleft()

最新更新