简体   繁体   中英

How to Stop Django Thread

I have a website that uses Jquery and Python.

I have an Ajax Request that performs a python function. Inside the Python Function there is an interface with Linux that executes a C++ Library.

The problem I am having is when I create two requests to execute this C++ Library from AJAX via Python. The C++ Task could take upto 2 minutes to load. This is fine however when I navigate away from the current page since the previous task has technically not completed Django will not load me a new page.

Is there any way I can determaine if the page is navigating away and if so then invoke python to stop its function so I can have some responsiveness.

One way I have thought of is threading however when I perform this approach if i Navigate away while a thread is still executing I can see from the debugger that it returns data even if I am not on the page that called the request.

So does anyone have any ideas how I can get python to kill all threads upon navigation away from its current page.

Cheers

Seems like you need an asynchronous execution for your "blocking" C++ Lib. There are several approaches for this kind of problems but IMO threading is deferentially the wrong one...

One approach is to use an asynchronous web server like Tornado to handle asynchronous requests. The other (and the one I like more) is to use Celery .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM