-1

lets say I have 5 users in a list, I will need to loop the list, for each user I will need to call some API to generate a txt file to store some JSON data.

Is it possible to do this 5 person at the same time with python? or any good recommendation or sample to help out? thanks a lot.

user1897151
  • 493
  • 2
  • 9
  • 28
  • I'm not sure why you would want to do either. In any case, try both, so that you can then ask more specific questions. Your's is pretty vague and of the kind "please teach me about X!", which is not the goal of SO. – Ulrich Eckhardt Oct 26 '22 at 06:30
  • Yes, obviously this is possible and there are plenty of questions and answers about threading and multiprocessing here. Please first do some research, and only ask _specific_ questions. We can't answer the question in the current form, since "some API" is too vague. We don't know if the API is thread-safe and if using multithreading or multiprocessing would gain any performance (which is presumably your goal) for that API. – wovano Oct 26 '22 at 06:38

2 Answers2

0

in python you can use library Threading

users = [1, 2, 3, 4, 5]
import threading

def call_api(end_point_api):
   "your code"


for user in users:
   end_point_api = ''
   start_thread = threading.Thread(target=call_api, args(end_point_api))
   start_thread.start()
0

The other answer is good, but you need to make sure you also join every thread in order to be sure your tasks ends when you want them.

users = [1, 2, 3, 4, 5]
import threading

def call_api(end_point_api):
   "your code"


thread_list = []
for user in users:
   end_point_api = ''
   start_thread = threading.Thread(target=call_api, args(end_point_api))
   start_thread.start()
   thread_list.append(start_thread)

for thread in thread_list:
    thread.join()

More here: Join in python

vladc
  • 150
  • 1
  • 9