Python execute threads by order

2020-05-01 14:52发布

I have the following code:

import threading

def send_to_server(lst):
    #Some logic to send the list to the server.


while 1:
    lst = []
    for i in range(1000):
        lst.append(i)
    task = threading.Thread(target=send_to_server,args(copy(lst),))
    task.start()

I have a few Questions:

1) The idea for using threads is because sending to server takes time and I want to continue
generating the data without any stop.
The problem with this code is that if I created thread #3 and its taking long time to process,
By that time thread #4 will be started. I want to assure that every list will be sent to server by the other I created it, means thread #3 will send to server the data before thread #4.
I understand that I need to use a Queue, but I don't know exactly how.

2)Should I use copy of lst? or I can use lst as well, I'm not sure.

1条回答
相关推荐>>
2楼-- · 2020-05-01 15:46

You want to use Queue, the threadsafe queue class in python. I imagine you want a thread to put things in the queue, and a thread to act on them serially like so:

q = Queue.Queue()
t1 = threading.Thread(target=fill_q,args(q, lst))
t2 = threading.Thread(target=consume_q,args(q, lst))
t1.start()
t2.start()

def fill_q(q, lst):
    for elem in lst:
        q.put(elem)

def consume_q(q, lst):
    for i in range(len(lst)):
        send_to_server(q.get())

If you are interested in performance, you may want to read up on the GIL in python, here https://wiki.python.org/moin/GlobalInterpreterLock

查看更多
登录 后发表回答