Search code examples
pythonmultithreadinggevent

Gevent thread not popping sequential parameters off list


I have the following in my flask app:

d = {'a': a, 'token':token}
import gevent.monkey
gevent.monkey.patch_socket()
threads = [gevent.spawn(myfunction, d) for i in range(2)]
result = gevent.joinall(threads)
print [thread.value for thread in threads]

I an trying to use multiple greenlets of the following function:

def myfunction(args):

    a= args['a']
    token= args['token']
    lo = list_object()

lo is set by :

def list_object():
        qlist = []
        # the following redis db lookup produces qlist=[a,b,c,......z]
        qlist = pr.query.order_by('failed').all() 
    return qlist.pop(0) 

I notice that although I was expecting each gevent threadlet to pop the first element off the list, so that running 2 threads (like in this example) would result in the first thread lo value being 'a', and the second 'b'. However I'm seeing Both values of lo being set to 'a' . Why is this happening? How can I fix this?

Edit:

qlist = []
d = {'a': a, 'token':token 'q':qlist}
.......

Solution

  • Is it because you instantiate qlist at the beginning of the function? So I think 'a' is being re-added by the pr.query.order_by().all(). Maybe make qlist a global variable and make a list parameter for list_object. Then 'a' should be popped out permanently. I could be wrong because I'm assuming pr.query.order_by.all() is adding 'a' to the list every time.