check a list constantly and do something if list has items

大城市里の小女人 提交于 2019-12-08 08:48:58

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/20231594/check-a-list-constantly-and-do-something-if-list-has-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!