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 intoitemdone
afterwards.You define the queue like this:
Then later you should include the queue in the arguments (or if you use threading, you can use global queue, like you did)
To stop the whole process, you can make the
additem
function to insert special token, and theproceeditems
, upon receiving the special token, will just quit the loop.