2

I am trying to call a python function by Ajax. I want to stop the execution of that function. I have tried to refresh the page but it doesn't stop. When I stopped the Django server then it get stop.

Here some code: This Call call HTTP request

class ScrapData():

    @classmethod
    def search_status(self, urls_list):
      for url in urls_list:
            r = requests.get(url)
            text = r.text
            #...

      return 1

Calling this view by Ajax

class SearchData(View):

    def get(self, request, *args, **kwargs):
        urls_list = [];
        response = ScrapData().search_status(urls_list)

        return HttpResponse(response)

I want to stop the function. How can I do this? Thanks

Shoaib Ijaz
  • 5,347
  • 12
  • 56
  • 84

3 Answers3

1

How is this django app served? How many processes and threads? How do you know which thread runs the particular request? There are several Python ways to deal with this, but it is not as simple as "stop a particular request".

In my opinion, the best way to deal with long-polling tasks in terms of management, scaleability and self-sanity is to use some tool like Celery. There is also a dedicated SO question on how to revoke a Celery task.

Django Channels is another fresh approach to the matter, but it needs a bit of more development yet before it can be used rather than Celery.

Community
  • 1
  • 1
Wtower
  • 18,848
  • 11
  • 103
  • 80
0

I got the same problem that I called a a wrong and slow request. And I stopped it by restarting my Apache server. It might be a solution when it's ok to restart the server.

JohnTsai
  • 33
  • 1
  • 7
0

try nested function, it will allow you to call whichever function you wish, or end process

the code:

class ScrapData():

    @classmethod
    def func():
        def search_status(self, urls_list):
              for url in urls_list:
                r = requests.get(url)
                text = r.text
                #...

          return 1
        def get(self, request, *args, **kwargs):
            urls_list = [];
            response = ScrapData().search_status(urls_list)

            return HttpResponse(response)
        def empty():
            pass
    #call the function you want to be excute further on
    #if you want nothing to happen then call empty function(pass keyword)