Working of Cache-mem using Queue in Python

All other Source.Python topics and issues.
SKY_SHY
Junior Member
Posts: 5
Joined: Sun Sep 18, 2016 9:51 am

Working of Cache-mem using Queue in Python

Postby SKY_SHY » Thu Sep 29, 2016 5:42 am

Hello! I write a code to simulate the working of cache-memory. In this model i try to realize FIFO-algorhythm which allow to delete last unused element (data, value, whatever).
I wrote a special function, which give me a list with numbers (these numbers are addresses in memory).

Syntax: Select all

q=Queue.Queue(800)# Cach - memory. This is queue which is more likely help me to simulate FIFO-algorhythm
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 is 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()


But i get the mistake: TypeError: argument of type 'instance' is not iterable at line

Code: Select all

if k not in q

Please help me to solve this problem!
Last edited by Ayuto on Thu Sep 29, 2016 11:11 am, edited 1 time in total.
Reason: code -> python
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: Working of Cache-mem using Queue in Python

Postby Doldol » Thu Sep 29, 2016 5:49 pm

You can't test for collection membership on Queues (aka you can't use 'in' on them).

Syntax: Select all

>>> 1 in queue.Queue()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'Queue' is not iterable


You could fix that by using deque (not thread-safe for iterations, obviously)
https://docs.python.org/3.5/library/col ... ions.deque

To add: Queues are meant to be used in threaded environments, you should use them when you want to block on putting something into or retrieving from the queue. In your case you really just want to be using something like deque.

More in-depth: http://stackoverflow.com/questions/7171 ... ions-deque

Edit: wait.... I'm not helping to solve your homework for you right? =/

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 23 guests