我有一个Python程序(确切地说,一个Django应用程序),它使用启动子进程subprocess.Popen.由于我的应用程序的架构限制,我无法使用Popen.terminate()终止子Popen.poll()进程并检查进程何时终止.这是因为我无法在变量中保存对已启动子进程的引用.
相反,我必须在子进程启动时将进程id写入pid文件pidfile.当我想停止子进程时,我打开它pidfile并用os.kill(pid, signal.SIGTERM)它来阻止它.
我的问题是:如何确定子进程何时真正终止?使用signal.SIGTERM它需要大约1-2分钟才能在通话后终止os.kill().首先,我认为这os.waitpid()对于这项任务来说是正确的,但是当os.kill()它给我之后我称之为它OSError: [Errno 10] No child processes.
顺便说一句,我使用两种形式从HTML模板开始和停止子进程,程序逻辑在Django视图中.当我的应用程序处于调试模式时,异常会显示在我的浏览器中.知道我在view(python manage.py crawlwebpages)中调用的子进程本身调用另一个子进程,即Scrapy爬虫的一个实例,这可能也很重要.我写了pid这个Scrapy实例pidfile,这就是我想要终止的.
这是相关代码:
def process_main_page_forms(request):
if request.method == 'POST':
if request.POST['form-type'] == u'webpage-crawler-form':
template_context = _crawl_webpage(request)
elif request.POST['form-type'] == u'stop-crawler-form':
template_context = _stop_crawler(request)
else:
template_context = {
'webpage_crawler_form': WebPageCrawlerForm(),
'stop_crawler_form': StopCrawlerForm()}
return render(request, 'main.html', template_context)
def _crawl_webpage(request):
webpage_crawler_form = …Run Code Online (Sandbox Code Playgroud)