Search code examples
pythonpython-2.7while-loopqueuegevent

How to kill all Greenlets or end "While not Queue.empty()" loop?


How to end Gevent without using sys.exit() in this case? I don't need to finish all the elements in the list, I just need to use the queue until a string is found.

tasks = Queue()
while not tasks.empty():
            string = tasks.get()
            con = validate(string)
            if con == True:
                break

Break statement is not working. I am starting the Greenlets like this:

gevent.spawn(worker) 

I cant use sys.exit() for the reason that I want to iterate though a list and start a new Gevent instance for every object.


Solution

  • I'm not familiar with python, but try using a boolean as an extra condition for your while loop:

    tasks = Queue()
    found = False
    while not found and not tasks.empty():
        string = tasks.get()
        found = validate(string)