I have a global list where items are added constantly (from network clients):
mylist = []
def additem(uuid,work):
mylist.append(uuid,work)
And a function which should check the list and if there are items proceed them:
def proceeditems():
while True:
itemdone = []
if len(mylist) > 0:
for item in mylist:
try:
#This can go wrong and than need to be done again
result = gevent.spawn(somework(item))
#result returns the uuid
itemdone.append(result.value)
except:
pass
for item in itemdone:
mylist[:] = [value for value in mylist if value[0]!=item]
So I hope you get an idea now what I try to do, but I think the endless loop seems to be not the right solution.
In this kind of case, you must have used either multithreading or multiprocessing (depending whether the network client is running at different thread or different process.
In either case, you should use Queue
to manage the incoming data, then store into itemdone
afterwards.
You define the queue like this:
my_queue = queue.Queue() # or multiprocessing.Queue()
Then later you should include the queue in the arguments (or if you use threading, you can use global queue, like you did)
def additem(uuid,work,the_queue):
the_queue.put((uuid,word)) # Queue in a tuple containing the data
def proceeditems(the_queue):
while True:
item = the_queue.get() # This will block until something is inside the queue
try:
result = somework(item)
itemdone.append(result)
except:
the_queue.put(item) # If failed, put back to queue for retry.
# You don't need the last two lines
To stop the whole process, you can make the additem
function to insert special token, and the proceeditems
, upon receiving the special token, will just quit the loop.