Forums

How can I recursively call one of my Classful methods as a completely new request since there is a response limit of 5 minutes per request?

As the title suggests I am trying to recursively call one of my class functions as a COMPLETELY NEW request. When I do the following it will recursively call the function correctly but it is called from within the SAME REQUEST.

return DBFunctions.addSongEnd(self, i, "1", finalSList, albumDict)

I have tried doing something similar to passing the data through requests.post but have troubled passing the data in various fashions:

  1. requests.post(url?varA?varB)
  2. requests.post(url, data={})
  3. requests.post(url, json={})
  4. requests.post(url, start=s, status=s2, albumD=albumDict, songL=songDict)
  5. requests.post(url_for(DBFunctions.addSongEnd(self, i, "1", finalSList, albumDict)))

I was not able to pass the data effectively using any of these which is why I have resorted to DBFunctions.addSongEnd, however I need a completely new request to be made

Do I have the right idea? Is there a better approach to go about this? Any suggestions are greatly appreciated

My class setup:

class DBFunctions(FlaskView):
    default_methods = ['GET', 'POST']
    def addSongEnd(self, start, status, songL, albumD):
        pass
        start = int(start)
        # START TIMER FOR FUNCTION
        startTime = time.perf_counter();

        if status == "0":
            finalSList = request.form['songList']
            finalSList = json.loads(finalSList)
            albumDict = {}
        else:
            finalSList = songL
            albumDict = albumD
        try:
            sessionUser = dbSession.query(User).filter_by(username=session['username']).scalar()
        except:
            dbSession.rollback()
            sessionUser = dbSession.query(User).filter_by(username=session['username']).scalar()

        for i in range(start, len(finalSList)):
            currentTime = time.perf_counter()
            timeDiff = currentTime - startTime
            if (timeDiff >= 10):
                # IF TIMER NOT EQUAL TO 290, call addSongFun with startingIndex
                # Try return redirect here
                return DBFunctions.addSongEnd(self, i, "1", finalSList, albumDict)

            else:
                songName = finalSList[i][0]
                artistName = finalSList[i][1][0]
                tP = finalSList[i][1][1]
                tC = finalSList[i][1][2]
                songResponse = list(json.loads(getSong(artistName, songName)))
                albumName = str(songResponse[0])
                uri = songResponse[1]
                if albumName not in albumDict.keys():
                    albumDict.update({albumName: [artistName, int(tP), int(tC), uri]})
                else:
                    albumDict[albumName][1] += int(tP)
                    albumDict[albumName][2] += int(tC)
                createdSong = Song(trackName = songName, artist = artistName, albumName = albumName, time_played = tP, clicks = tC, song_uri=uri)
                sessionUser.songLibrary.append(createdSong)
                try:
                    dbSession.commit()
                except:
                    dbSession.rollback()
                    dbSession.commit()

        sortedAlbumList = dict(sorted(albumDict.items(), key=lambda item: item[1][1], reverse=True))
        return json.dumps(sortedAlbumList, separators=(',', ':'))

You cannot do that in a web app. If you make a request to a different endpoint, the first request will have to wait for the second one to return, so it will be the same as if you ran all the code in a single request.

Depending on your app, you might be able to make the subsequent requests from the browser (either through user-interaction or using javascript), then each request would be independent.