Create a thread for each element in list

Question:

I am trying to make for each item in my list a separate thread.

How can i achieve this?

List contains 10-20 urls. Which i need to download,parse and insert to DB

So what i am trying to do:

urls = {'url1,url2,url3'}
def get_and_insert(xml):
    try:
        get = requests.get(xml)
        parsed = xmltodict.parse(get.text)
        //upload info to DB and so on..


for each in urls:
      threading.Thread(target=get_and_insert(each)).start()

But python still going one by one each url. without threading.

How to do it? Or maybe someone can share an example with async.

Asked By: Kappa

||

Answers:

Problem was in

threading.Thread(target=get_and_insert(each)).start()

I pass argument to thread incorrectly

Correct way to do it:

threading.Thread(target=get_and_insert,args=(each,)).start()

Since args should get tuple

Now it works

Answered By: Kappa

You need to send the arguments in args, don’t forget to close the threads!

threads = []
urls = {'url1,url2,url3'}

for each in urls:
    t = threading.Thread(target=get_and_insert, args=(each,))
    t.start()
    threads.append(t)

for t in threads:
    t.join()
    print(t.is_alive())