Forums

Codes creates exponential time decrease

Hi all, I have this code here that loops through a user's input on the template and saves each individual item separately on the view (113 times). The first iteration finishes in .015 seconds, the third in 9.8 seconds the eight in 19.8, the 15th in 30 seconds. This continues all the way to the last one which ends at 209 seconds to loop the code.It then ends with this:

2017-01-25 12:24:51 Wed Jan 25 12:24:51 2017 - uwsgi_response_writev_headers_and_body_do(): Broken pipe [core/writer.c line 287] during POST /slotUploader/055001/ (10.0.0.14)
2017-01-25 12:24:51 RuntimeError
2017-01-25 12:24:51 : 
2017-01-25 12:24:51 generator ignored GeneratorExit
2017-01-25 12:24:51

And the Code:

 if brand == None:
        bedDB2 = FloorTracker.objects.filter(showroom_number=strNum)
    else: #check if records exist, yes continue, no goto except
        bedDB2 = FloorTracker.objects.filter(showroom_number=strNum).filter(brand=brand) #check if records exist, yes continue, no goto except
    print('tracked down store exists with data')
    store = storeList.objects.get(storenumber=strNum)
    for mega, company, family, bed, sretail in beds:
        bedDB2 = FloorTracker.objects.get(showroom_number=strNum,mattress=bed)
        print ('tracked down original')
        bedDB = FloorTracker(
            user = store.user,
            showroom_number = store,
            brand = company,
            family = family,
            mattress = bed,
            company = mega,
            suggested_retail = sretail,
            on_floor = 0 if request.POST.get('cb_' + bed) == None else 1,
            in_comparison = 0 if request.POST.get('cb_' + bed + '_comparison') == None else 1,
            in_vzone = 0 if request.POST.get('cb_' + bed + '_vzone') == None else 1,
            size = "" if request.POST.get('sz_' + bed) == 'blank' or not request.POST.get('sz_' + bed) else request.POST.get('sz_' + bed),
            underbed = "" if request.POST.get('ub_' + bed) == 'blank' or not request.POST.get('ub_' + bed) else request.POST.get('ub_' + bed),
            lastupdate = str(datetime.today().date()),
            id = bedDB2.id
        )
        bedDB.save(force_update=True)
        t1 = time.time()
        total = t1-t0
        print('Success', bedDB.mattress,total)

Thanks in advance!

Rich

the generator exit error is for when your website takes too long to fully load and the user navigates away from it, cutting/stopping the page before it is fully returned

Right, I get that part. Which is somehow being caused by that. code.

I just compacted the code down to this and I still get the same results.

beds = bedSelect(brand)
store = storeList.objects.get(storenumber=strNum)
for mega, company, family, bed, sretail in beds:
    try:
        matt = FloorTracker.objects.get(showroom_number=strNum,mattress=bed)
        matt.brand = company
        matt.family = family
        matt.company = mega
        matt.suggested_retail = sretail
        matt.on_floor = 0 if request.POST.get('cb_' + bed) == None else 1
        matt.in_comparison = 0 if request.POST.get('cb_' + bed + '_comparison') == None else 1
        matt.in_vzone = 0 if request.POST.get('cb_' + bed + '_vzone') == None else 1
        matt.size = "" if request.POST.get('sz_' + bed) == 'blank' or not request.POST.get('sz_' + bed) else request.POST.get('sz_' + bed)
        matt.underbed = "" if request.POST.get('ub_' + bed) == 'blank' or not request.POST.get('ub_' + bed) else request.POST.get('ub_' + bed)
        matt.lastupdate = str(timezone.now())
        matt.save()
    except FloorTracker.DoesNotExist:
        matt = FloorTracker(
        user = store.user,
        showroom_number = store,
        brand = company,
        family = family,
        mattress = bed,
        company = mega,
        suggested_retail = sretail,
        on_floor = 0 if request.POST.get('cb_' + bed) == None else 1,
        in_comparison = 0 if request.POST.get('cb_' + bed + '_comparison') == None else 1,
        in_vzone = 0 if request.POST.get('cb_' + bed + '_vzone') == None else 1,
        size = "" if request.POST.get('sz_' + bed) == 'blank' or not request.POST.get('sz_' + bed) else request.POST.get('sz_' + bed),
        underbed = "" if request.POST.get('ub_' + bed) == 'blank' or not request.POST.get('ub_' + bed) else request.POST.get('ub_' + bed),
        lastupdate = str(timezone.now())
        )
        matt.save()

That may be related to the database trouble we had yesterday. Is it still showing that behaviour?