How to get data from a running task in Python(Django)

I have a script to compare and move my files around, which is to be called by

cmp_object = my_cmp.Compare('/mnt/f/somedir', '/mnt/f/newdir')
cmp_object.main()

and the Compare class is defined as

class Compare:
    def __init__(self, path1, path2):
        self.path1 = path1
        self.path2 = path2
        self.totalFileNumber = getTotalFileNumber(path1)
        self.totalFileSize = getTotalFileSize(path1)
        self.progress = 0
        self.progressPercent = 0
        self.status = 'Not started'
        self.started = False
        self.finished = False

    def start(self):
        self.status = 'Started'
        self.started = True
        self.finished = False

    def update(self, index):
        self.progress = index
        self.progressPercent = round(
            self.progress / self.totalFileNumber * 100, 2)
        self.status = f'{self.progressPercent}%'
        if self.progressPercent == 100:
            self.status = 'Finished'
            self.finished = True

    def getStatus(self):
        return self.status

    def getProgress(self):
        return self.progressPercent

    def getTotalFileNumber(self):
        return self.totalFileNumber

    def getTotalFileSize(self):
        return self.totalFileSize

    def isStarted(self):
        return self.started

    def isFinished(self):
        return self.finished

    def main(self):
        self.start()
        for index, file in enumerate(listAllFiles(self.path1)):
            compareFiles(file, self.path2 + file[len(self.path1):])
            self.update(index)
        self.update(self.totalFileNumber)
        self.finished = True

, which has functions to get and set some states like progress, status and finish. I am running this on Django backend, where I would be retrieving information from the backend by setting an interval...

I am using cache to set and get the states across different views, but I'm not sure how can I keep on updating the cache while the main operation is running?

my view:

@api_view(['GET'])
def start_sync(request):
    cmp_object = my_cmp.Compare('/mnt/f/somedir', '/mnt/f/newdir')
    cache.set('status', 'started', 100)
    # part 1
    cmp_object.main()
    # part 2
    while not cmp_object.isFinished():
        print(cmp_object.getStatus())
        cache.set('total_files', cmp_object.getTotalFileNumber(), 100)
        cache.set('total_size', cmp_object.getTotalFileSize(), 100)
        cache.set('progress', cmp_object.getProgress(), 100)
        cache.set('status', cmp_object.getStatus(), 100)
        time.sleep(1)
    cache.set('status', 'finished', 100)
    return Response({'done': True})

What I want is to execute task 2 while task 1 is running(copying files)... Would using celery be a good choice to solve this problem?

Option 1 is you can call the task 2 function in the task 1 function. Option 2 would to use celery. However, Celery is more for asynchronous work. Celery would be useful if you wanted to run a task without the user having to wait for it to finish.

Back to Top