I have a list with more than 500 elements, and I want to run a function for each element in the list at the same time, how can I do that?
That solution will speed up the process, and also, my code will be shorter.
Im using one lib for Instagram, and getting the values from that lib requires a lot of time. I have managed the speed it with threads but im dealing with large number of values in list. Each element is an username, so I want to go to profile of 500 users at the same time and collect some data from it (post picture, number of comments, number of likes...).
For each user it takes around 1-2 seconds for collecting data. That means that collecting data for 1000 users will take 1000-2000 seconds. So Im trying to make this faster by running the same function at the same time for every item in list.
I was reading a little about threading
and I have made something like this, but what if I have 500 elements in list:
import threading
list_of_results = []
list_of_values = [1,2,3]
def one(list_of_values, list_of_results):
result = 0
for i in range(0,5000):
result = list_of_values[0] + i
list_of_results.append(result)
def two(list_of_values, list_of_results):
result = 0
for i in range(0,5000):
result = list_of_values[1] + i
list_of_results.append(result)
def three(list_of_values, list_of_results):
result = 0
for i in range(0,5000):
result = list_of_values[2] + i
list_of_results.append(result)
t1 = threading.Thread(target = one, args = (list_of_values, list_of_results))
t2 = threading.Thread(target = two, args = (list_of_values, list_of_results))
t3 = threading.Thread(target = three, args = (list_of_values, list_of_results))
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
print(list_of_results)