I'm trying to save a really big .csv
file from upload form
in Django into MongoDB
, it works, but it takes too much time to process, so I decided to use Multiprocessing, this approach shorted the time a little bit, but CPU cores came to 100%, so I would like to make my code a little bit lower-computation expensive. Here is my code
views.py
def save_to_db(line): # I think this part cost the most time
column = line.split(",")
kokyaku.objects.create(顧客CD = int(column[0]), 顧客補助CD = int(column[1]),
顧客名称s=str(column[2]), 顧客名称=str(column[3]),
顧客名称カナ=str(column[4]), 法人名称=str(column[5]),
代表者名称=str(column[6]),住所=str(column[7]),
電話番号=str(int(column[8])),地区名称=str(column[9]),
データマッチ用電話番号=int(column[10]),契約状態=str(column[11])
)
def upload(request):
data = {}
if "GET" == request.method:
return render(request, "main/upload.html", data)
# if not GET, then proceed
csv_file = request.FILES["csv_file"]
file_type = request.POST.get("type", "")
if not csv_file.name.endswith('.csv'):
messages.error(request,'File is not CSV type')
return HttpResponseRedirect(reverse("upload"))
file_data = csv_file.read().decode("utf-8")
lines = file_data.split("\n")
if file_type == "val3":
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(save_to_db, lines)
return HttpResponseRedirect(reverse("upload"))
btw. here is my class from models.py
If it helps too
class kokyaku(models.Model):
顧客CD = models.IntegerField(blank=True)
顧客補助CD = models.IntegerField(blank=True)
顧客名称s = models.TextField(blank=True)
顧客名称 = models.TextField(blank=True)
顧客名称カナ = models.TextField(blank=True)
法人名称 = models.CharField(max_length=15, blank=True)
代表者名称 = models.CharField(max_length=15, blank=True)
住所 = models.TextField(blank=True)
地区名称 = models.TextField(blank=True)
電話番号 = models.IntegerField(blank=True)
データマッチ用電話番号 = models.IntegerField(blank=True)
契約状態 = models.CharField(max_length=2, blank=True)
def __str__(self):
string = str(self.顧客CD) + " - " + self.顧客名称
return string