Python Django创建工作线程并停止工作线程

时间:2018-10-08 14:59:37

标签: python django multithreading

我对Django Python中的线程有疑问。我想用两个按钮创建一个视图,第一个按钮在后台启动模拟,第二个按钮可以在后台随时停止模拟。但是我已经尝试使用threading.Threads()以及background_tasks,但是我没有管理它随时停止进程。有可能吗?

查看功能:

def change_calculating_status(request, component_id):
    try:
        obj = get_object_or_404(Component, pk=component_id)
    except ValueError as e:
        print(e)
        raise Http404

    if(obj.calculation_running == True):
        obj.calculation_running = False

    elif(obj.calculation_running == False):
        obj.calculation_running = True


        if(obj.shortest_path == False):
            obj.shortest_path = True
            sp.run_simulation()

    obj.save()
    return HttpResponseRedirect(reverse('component',args=(component_id,)))

HTML模板:

{% if component.calculation_running == True %}
        <form action="change_calculating_status">
            {% csrf_token %}
            <input type="submit" id="start_calculation" onclick="disablestartenablestop()" value="Start Calculation" disabled=True />
            <input type="submit" id="stop_calculation" onclick="enablestartdisablestop()" value="Stop Calculation" />
        </form>
{% else %}
        <form action="change_calculating_status">
            {% csrf_token %}
            <input type="submit" id="start_calculation" onclick="disablestartenablestop()" value="Start Calculation"/>
            <input type="submit" id="stop_calculation" onclick="enablestartdisablestop()" value="Stop Calculation" disabled=True/>
        </form>
{% endif %}

非常感谢您!

最诚挚的问候

Blacktiger800

0 个答案:

没有答案