在一个多年前的历史项目中,我们发现,django cron job出现了卡死现象。 应用逻辑没有被完整执行直到结束,程序已经僵死了。
调试分析卡死的Python进程,请参考: https://wiki.python.org/moin/DebuggingWithGdb
通过调试进程,我们发现应用卡死在了
Domain.objects.bulk_update(update_domain_list, [‘ips_from_fuxi’])
如下图所示:

回到源代码中,在上下文找到了原因,原来,需要bulk update的列表,没有在循环体内部正确被初始为空。 需要update的列表出现了持续增长,最终导致bulk update 超大的列表,卡死应用程序。

正确初始化列表,即可解决bulk update hung死的问题。