我创建了可以运行 jupyter 笔记本服务器的 Flask 应用程序。
当我们从 UI 中单击应用程序时,将使用 subprocess.Popen() 创建一个新的子进程来构建 Jupyter 笔记本服务器。 我使用 win32serviceutil 为 Flask Web 应用程序创建了 Windows 服务。 共有三种情况。
场景 1:当使用 python.exe 运行 Flask 应用时,Jupyter Notebook 可以正常工作。
场景2:Flask应用服务在debug模式下执行时,运行正常。
command :-
python service.py debug
场景 3:- 当 windows 服务 jupyter 笔记本内核死亡时执行 Flask 应用程序时。
Error :-
Traceback (most recent call last):
File "d:\v307\lib\site-packages\tornado\web.py", line 1704, in _execute
result = await result
File "C:\Program Files\Python39\lib\asyncio\tasks.py", line 328, in __wakeup
future.result()
File "d:\v307\lib\site-packages\tornado\gen.py", line 769, in run
yielded = self.gen.throw(*exc_info) # type: ignore
File "d:\v307\lib\site-packages\notebook\services\sessions\handlers.py", line 74, in post
model = yield maybe_future(
File "d:\v307\lib\site-packages\tornado\gen.py", line 762, in run
value = future.result()
File "d:\v307\lib\site-packages\tornado\gen.py", line 769, in run
yielded = self.gen.throw(*exc_info) # type: ignore
File "d:\v307\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 98, in create_session
kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
File "d:\v307\lib\site-packages\tornado\gen.py", line 762, in run
value = future.result()
File "d:\v307\lib\site-packages\tornado\gen.py", line 769, in run
yielded = self.gen.throw(*exc_info) # type: ignore
File "d:\v307\lib\site-packages\notebook\services\sessions\sessionmanager.py", line 110, in start_kernel_for_session
kernel_id = yield maybe_future(
File "d:\v307\lib\site-packages\tornado\gen.py", line 762, in run
value = future.result()
File "C:\Program Files\Python39\lib\asyncio\futures.py", line 201, in result
raise self._exception
File "C:\Program Files\Python39\lib\asyncio\tasks.py", line 256, in __step
result = coro.send(None)
File "d:\v307\lib\site-packages\notebook\services\kernels\kernelmanager.py", line 176, in start_kernel
kernel_id = await maybe_future(self.pinned_superclass.start_kernel(self, **kwargs))
File "d:\v307\lib\site-packages\jupyter_client\utils.py", line 30, in wrapped
raise e
File "d:\v307\lib\site-packages\jupyter_client\utils.py", line 27, in wrapped
return loop.run_until_complete(future)
File "d:\v307\lib\site-packages\nest_asyncio.py", line 89, in run_until_complete
return f.result()
File "C:\Program Files\Python39\lib\asyncio\futures.py", line 201, in result
raise self._exception
File "C:\Program Files\Python39\lib\asyncio\tasks.py", line 256, in __step
result = coro.send(None)
File "d:\v307\lib\site-packages\jupyter_client\multikernelmanager.py", line 212, in _async_start_kernel
starter = ensure_async(km.start_kernel(**kwargs))
File "d:\v307\lib\site-packages\jupyter_client\utils.py", line 30, in wrapped
raise e
File "d:\v307\lib\site-packages\jupyter_client\utils.py", line 27, in wrapped
return loop.run_until_complete(future)
File "d:\v307\lib\site-packages\nest_asyncio.py", line 89, in run_until_complete
return f.result()
File "C:\Program Files\Python39\lib\asyncio\futures.py", line 201, in result
raise self._exception
File "C:\Program Files\Python39\lib\asyncio\tasks.py", line 256, in __step
result = coro.send(None)
File "d:\v307\lib\site-packages\jupyter_client\manager.py", line 83, in wrapper
raise e
File "d:\v307\lib\site-packages\jupyter_client\manager.py", line 75, in wrapper
out = await method(self, *args, **kwargs)
File "d:\v307\lib\site-packages\jupyter_client\manager.py", line 383, in _async_start_kernel
kernel_cmd, kw = await ensure_async(self.pre_start_kernel(**kw))
File "d:\v307\lib\site-packages\jupyter_client\utils.py", line 30, in wrapped
raise e
File "d:\v307\lib\site-packages\jupyter_client\utils.py", line 27, in wrapped
return loop.run_until_complete(future)
File "d:\v307\lib\site-packages\nest_asyncio.py", line 89, in run_until_complete
return f.result()
File "C:\Program Files\Python39\lib\asyncio\futures.py", line 201, in result
raise self._exception
File "C:\Program Files\Python39\lib\asyncio\tasks.py", line 256, in __step
result = coro.send(None)
File "d:\v307\lib\site-packages\jupyter_client\manager.py", line 349, in _async_pre_start_kernel
kw = await self.provisioner.pre_launch(**kw)
File "d:\v307\lib\site-packages\jupyter_client\provisioning\local_provisioner.py", line 190, in pre_launch
km.write_connection_file()
File "d:\v307\lib\site-packages\jupyter_client\connect.py", line 503, in write_connection_file
self.connection_file, cfg = write_connection_file(
File "d:\v307\lib\site-packages\jupyter_client\connect.py", line 164, in write_connection_file
with secure_write(fname) as f:
File "C:\Program Files\Python39\lib\contextlib.py", line 117, in __enter__
return next(self.gen)
File "d:\v307\lib\site-packages\jupyter_core\paths.py", line 893, in secure_write
win32_restrict_file_to_user(fname)
File "d:\v307\lib\site-packages\jupyter_core\paths.py", line 442, in win32_restrict_file_to_user
user, _domain, _type = win32security.LookupAccountName(
pywintypes.error: (1789, 'LookupAccountName', 'The trust relationship between this workstation and the primary domain failed.')
我使用 waitress 作为网络服务器,并使用 win32serviceutil 来创建 Flask 应用程序的服务
Python 版本 3.9
期待
当我们将 Flask webapp 作为服务运行时解决问题的方法。
在 Windows 操作系统中,所有服务都在会话 0 上运行(👉 会话 0) 会话 0 无法直接访问 GPU 这就是 Jupyter 内核显示错误的原因。 Windows 服务可以选择以其他用户身份登录,并允许用户直接访问桌面(👉 有关服务登录的更多信息 ,👉有关桌面交互服务的更多信息)。此选项没有解决我的问题。
解决方案是让 Flask Server 在终端中运行。