Let me start with saying that I've never used threading in Python. I have a list containing about 8000 dictionaries. It contains data about ingredients, their calories, nutrients etc.
for index in range(len(listOfFilteredFoods)):
for key in listOfFilteredFoods[index]:
if(listOfFilteredFoods[index][key] == None):
listOfFilteredFoods[index][key] = 0.0
requests.post(url, data=listOfFilteredFoods[index])
This loop checks if any field (vitamin A, potassium or other) is equal to None then it just overwrites is with 0.0. Then each dictionary is POSTed to my Django database. Problem is this 8k list took about 10minutes to complete. If i were to use threads will there be duplicates of same ingredient instances?
Edit
The dictionary in question looks like this (abbreviation):
[
{
"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,
}
]
CodePudding user response:
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)
CodePudding user response:
2 points.
1 - If you call this loop more one times (eg: by button action) thread will start again. So yes "will there be duplicates of same ingredient"
2 - If you only put this loop in thread function, no you not "will there be duplicates of same ingredient", because thread will running in "background", but sequential.
