Popen()有时在_cleanup()中失败,出现“句柄无效”

我正在做一个Django项目。 其中一个观点叫做Popen()。 大多数情况下,一切正常。 但是偶尔Popen()会失败并显示以下错误消息:

Traceback (most recent call last): File "C:\***\views.py", line 116, in process_request proc = Popen(['python', exec_path, input_excel, current_tab, project]) File "C:\Python34\lib\subprocess.py", line 754, in __init__ _cleanup() File "C:\Python34\lib\subprocess.py", line 474, in _cleanup res = inst._internal_poll(_deadstate=sys.maxsize) File "C:\Python34\lib\subprocess.py", line 1146, in _internal_poll if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0: OSError: [WinError 6] The handle is invalid 

重新启动服务器通常可以解决问题,但稍后可能会再次显示。 失败后立即尝试失败通常也会失败(用循环实现)。 手动重新加载页面多次有时解决问题。

我也试过64位和32位的Python版本。 问题都显示在两者上。

似乎_cleanup()pipe理用于避免僵尸进程的_active列表。 考虑到我在windows上,我注释到了Popen()中的_cleanup(),它似乎目前工作正常。 显然这不是一个适当的修复。 更好的主意?

更新:

在听到他的build议之后,我仔细地看着把手。 似乎进程句柄由于某种原因被django的autoreload.pyclosures了。 详情请参阅下文。

 --------------------------------------------------- try handle: Handle(908) File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line utility.execute() File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 330, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python34\lib\site-packages\django\core\management\base.py", line 393, in run_from_argv self.execute(*args, **cmd_options) File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 49, in execute super(Command, self).execute(*args, **options) File "C:\Python34\lib\site-packages\django\core\management\base.py", line 444, in execute output = self.handle(*args, **options) File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 88, in handle self.run(**options) File "C:\Python34\lib\site-packages\django\core\management\commands\runserver.py", line 97, in run autoreload.main(self.inner_run, None, options) File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 325, in main reloader(wrapped_main_func, args, kwargs) File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 291, in python_reloader reloader_thread() File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 267, in reloader_thread change = fn() File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 204, in code_changed for filename in gen_filenames(): File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 92, in gen_filenames _cached_filenames = clean_files(_cached_filenames) File "C:\Python34\lib\site-packages\django\utils\autoreload.py", line 139, in clean_files if os.path.exists(filename): File "C:\Python34\lib\genericpath.py", line 19, in exists os.stat(path) File "C:\Python34\lib\subprocess.py", line 452, in Close print(traceback.print_stack()) None handle closed. --------------------------------------------------- 

上面的错误信息是由下面的修改产生的。

 def Close(self, CloseHandle=_winapi.CloseHandle): print ('---------------------------------------------------') print ('try handle:') print (self) if not self.closed: self.closed = True CloseHandle(self) print(traceback.print_stack()) print ('handle closed.') print ('---------------------------------------------------') 

后来exception抱怨Handle(908)。 我不能完全遵循os.stat(path)如何closures句柄,以及为什么进程不会被subprocess.py从_active列表中移除。

Solutions Collecting From Web of "Popen()有时在_cleanup()中失败,出现“句柄无效”"