title: How to get data from a running task in Python(Django)
body: <p>I have a script to compare and move my files around, which is to be called by</p>
  <pre class="lang-py prettyprint-override"><code>cmp_object = my_cmp.Compare('/mnt/f/somedir', '/mnt/f/newdir')
  cmp_object.main()
  </code></pre>
  <p>and the Compare class is defined as</p>
  <pre class="lang-py prettyprint-override"><code>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
  </code></pre>
  <p>, 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...</p>
  <p>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?</p>
  <p>my view:</p>
  <pre class="lang-py prettyprint-override"><code>@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})
  </code></pre>
  <p>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?</p>