POST request in loop using threading. Will there be duplicates?
Solution 1:
Obviously I have no way to test this but this pattern of execution is probably what you need.
import requests
from concurrent.futures import ThreadPoolExecutor
listOfFilteredFoods = [
{
"name": "#1 Ingredient name",
"calories": 100.0,
"nutrient#1": 10.0,
"nutrient#2": 20.0,
},
{
"name": "#8000 Ingredient name",
"calories": 100.0,
"nutrient#1": 10.0,
"nutrient#2": 20.0,
}
]
url = 'myFavouriteURL'
def doPOST(data):
with requests.Session() as session:
session.post(url, data=data).raise_for_status()
with ThreadPoolExecutor() as executor:
try:
for d in listOfFilteredFoods:
for k in d.keys():
if d[k] == None:
d[k] = 0.0
executor.submit(doPOST, d)
finally:
executor.shutdown(wait=True)