问题
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:
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
来源:https://stackoverflow.com/questions/35253751/python-execute-threads-by-order