I'm looking to send 100K-300K POST requests to an API Endpoint - these requests are originating from a list of JSON objects that I'm iterating over. Unfortunately, the maximum chunk size I'm able to use is 10 events at a time which is greatly decreasing the speed of sending off all the events I want. After I have my list of JSON objects defined:
chunkSize= 10
for i in xrange(0, len(list_of_JSON), chunkSize):
chunk = list_of_JSON[i:i+chunkSize] #10
endpoint = ""
d_profile = "[" + ",".join(json.dumps(x) for x in chunk) + "]"
str_event = d_profile
try:
url = base_api + endpoint + "?api_key=" + api_key + "&event=" + str_event
r = requests.post(url)
print r.content
print i
except:
print 'failed'
This process works extremely slowly to send off the events. I've looked up the possibility of multithreading/concurrency/and parallel processing although I'm completely new to the topic. After some research, I've come up with this ugly snippit:
import logging
import threading
import time
logging.basicConfig(level=logging.DEBUG,
format='[%(levelname)s] (%(threadName)-10s) %(message)s',
)
def worker():
logging.debug('Starting')
import time
chunkSize= 10
for i in xrange(0, (len(list_of_JSON)/2), chunkSize):
chunk = list_of_JSON[i:i+chunkSize] #10
endpoint = ""
d = "[" + ",".join(json.dumps(x) for x in chunk) + "]"
str_event = d
try:
url = base_api + endpoint + "?api_key=" + api_key + "&event=" + str_event
r = requests.post(url)
print r.content
print i
except:
print 'failed'
time.sleep(2)
logging.debug('Exiting')
def my_service():
logging.debug('Starting')
import time
chunkSize= 10
for i in xrange(((len(list_of_JSON)/2)+1), len(list_of_JSON), chunkSize):
chunk = list_of_JSON[i:i+chunkSize] #10
endpoint = ""
d = "[" + ",".join(json.dumps(x) for x in chunk) + "]"
str_event = d
try:
url = base_api + endpoint + "?api_key=" + api_key + "&event=" + str_event
r = requests.post(url)
print r.content
print i
except:
print 'failed'
time.sleep(3)
logging.debug('Exiting')
t = threading.Thread(target=my_service)
w = threading.Thread(target=worker)
w.start()
t.start()
Would appreciate any suggestions or refactoring.
Edit: I believe my implementation accomplishes what I want. I've looked over What is the fastest way to send 100,000 HTTP requests in Python? but am still unsure of how pythonic or efficient this solution is.