Python在主进程被终止时杀死所有子进程

时间:2018-04-10 17:36:11

标签: python multiprocessing

主进程通过使用multiprocessing.Process()创建了几个子进程。子进程还会创建自己的子进程。以下是示例代码:

// @method setFocus sets focus on input when clicked. Needed as FF won't focus if quantity is updated from spinners
// @return {Void}
,   setFocus: function setFocus (e)
{

    var value = parseInt(e.currentTarget.value, 10)
    , qty_avail = this.model.getStockInfo().stock
    , $input_quantity = this.$('[name="quantity"]');

    if(value > qty_avail)
    {

        e.preventDefault();
        this.$('.product-details-quantity-alert').html("There is only " + qty_avail + " piece(s) available with these options");
        this.$('.product-details-quantity-alert').css("display", "initial");

    }
    else
    {
        this.$('.product-details-quantity-alert').css("display", "none");
        $input_quantity.focus();
    }

    console.log(qty_avail);
    console.log(value);

}

我的问题主要是关于如何在程序退出问题时清理所有进程。问题可能是由信号导致的进程或某些子进程获得异常。我基本上想要清理所有进程,如果有任何进程被解决问题。我使用的是python2.7。

1 个答案:

答案 0 :(得分:0)

daemon标志是你需要的。如果主进程退出,守护进程子进程将被终止。

反过来说,唯一可行的方法是让家长定期检查孩子是否仍然通过他们的is_alive方法运行。

相关问题