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.
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:If you are interested in performance, you may want to read up on the
GIL
in python, here https://wiki.python.org/moin/GlobalInterpreterLock