Forums

Abort request A from request B

I have a query that takes a long time to complete. Assume that request A inititates such a query. Then another request B comes, from the same client, and inititates another instance of that query. I want the query initiated by request A aborted.

Note here that those queries are to an sqlite database.

The only way in which I can think of achieving what I want is to kill the thread that is handling request A. Is it possible to do this withing the PAW environment?

We did a little research on this for a view in PythonAnywhere, but eventually came to the conclusion that it's just not possible in any reliable way. We eventually used a very application-specific workaround. One query that takes a long time, makes it really hard. In that case, I can't think of a way that wouldn't risk destroying your database. If you could change the query so that it was batched into smaller pieces, you could have an interrupt flag set by the second request that the first one could look for and stop if it was set.

We did a little research on this for a view in PythonAnywhere, but eventually came to the conclusion that it's just not possible in any reliable way. We eventually used a very application-specific workaround. One query that takes a long time, makes it really hard. In that case, I can't think of a way that wouldn't risk destroying your database.

Well if there was a way in Python to kill an arbitrary thread then I could store each request along with its thread number and then use that information to kill that thread. Is this not possible within your framework?

Looks like there is already sqlite support for the feature I want:

If the progress callback returns non-zero, the operation is interrupted. This feature can be used to implement a "Cancel" button on a GUI progress dialog box.

There are no threads in PythonAnywhere web apps. We manage everything at the process level and your web app doesn't have the permissions necessary to kill processes. Also, I would think that randomly killing threads (or processes) during database accesses would have a real risk of destroying your database.

The sqlite progress callback could work, as long as the callback was checking something that's shared between workers (which are different processes on PythonAnywhere). For instance, the existence of a file. Returning based on a variable set in the code would probably not work because each of the requests would be handled by a different worker in a different process.