kizus
kizus

Reputation: 37

Stopping function processing externally

I have a huge problem. I am working in a Web Python Project, where, after click in a button, a specific controller is called and then another function, present in a python module, is called as well, as shown in my code below. However , I need a second button that stops the process of the stream function controller.

import analyser
def stream():
    analyser.get_texts()
    response.flash = "Analysis Done."
    return ""

I've been searching a lot how to stop a process by an external event (something similar to interruption), but the solutions that I've got, all of them, were about how to stop python script using sys.exit() ou programatically by a return statement, for example. None of these solutions actually work for me.

I want that the user be able to stop that function whenever he wants, once that my function analyser.get_texts() remains processing all the time.

So, my question is how can I stop the execution of stream function, through a button click on my view? Thanks.

Upvotes: 1

Views: 60

Answers (1)

Florian Brucker
Florian Brucker

Reputation: 10365

If I understand you correctly, then your analyser doesn't provide its own way to terminate an ongoing calculation. You will therefore need to wrap it into something that allows you to terminate the analyser without its "consent".

The right approach for that depends on how bad terminating the analyser in that way is: does it leave resources in a bad state?

Depending on that, you have multiple options:

  • Run your analysis in a separate process. These can be cleanly killed from the outside. Note that it's usually not a good idea to forcefully stop a thread, so use processes instead.

  • Use some kind of asynchronous task management that lets you create and stop tasks (e.g. Celery).

Upvotes: 1

Related Questions