CEFPython window 传输大数据时无警告关闭
CEFPython window closed without warning when transfering large data
我正在尝试创建此 python 绑定到我创建的 electronjs 应用程序,其中 python 部分用于分析数据和结果(python 字典)被传递到网络浏览器进行可视化。对于少量数据,这一切都非常有效,但是当我尝试传递一个大的结果字典(~200MB)时,创建了 window,但在终端中没有警告就关闭了(我无法检查 devtool ),尽管后端似乎有一个 subprocess 保留 运行。 windows 和 Ubuntu 机器上都出现了这个问题,非常感谢你的帮助。
这是我的代码:
def view(data):
config = data
settings = {
"debug": True,
"log_severity": cef.LOGSEVERITY_INFO,
"log_file": "debug.log",
}
cef.Initialize(settings=settings)
browser_setting = { "file_access_from_file_urls_allowed":True,\
"universal_access_from_file_urls_allowed": True,\
"web_security_disabled":True}
browser = cef.CreateBrowserSync(url='file://' + os.path.realpath("index_cefpython.html"),
window_title="Javascript Bindings", settings = browser_setting)
browser.SetClientHandler(LoadHandler(config))
bindings = cef.JavascriptBindings()
browser.SetJavascriptBindings(bindings)
cef.MessageLoop()
del browser
cef.Shutdown()
return
class LoadHandler(object):
def __init__(self, config):
self.config = config
def OnLoadEnd(self, browser, **_):
browser.ExecuteFunction("defineData", self.config)
在 JS 方面,我有:
<script type="text/javascript">
function defineData(datainput){
console.log("start")
data = datainput;
Main();
}
</script>
这是终端中打印出的所有消息:
[0312/104311.439:INFO:cefpython_app.cpp(199)] [Browser process] OnBeforeChildProcessLaunch() command line: "<>\Anaconda3\lib\site-packages\cefpython3\subprocess" --type=gpu-process --no-sandbox --locales-dir-path="<>\Anaconda3\lib\site-packages\cefpython3\locales" --log-file=debug.log --log-severity=info --resources-dir-path="<>\Anaconda3\lib\site-packages\cefpython3" --lang=en-US --disable-gpu-shader-disk-cache /prefetch:2
[0312/104311.440:INFO:cef_log.cpp(8)] [Browser process] Switch already set, ignoring: disable-gpu-shader-disk-cache
[0312/104311.440:INFO:cefpython_app.cpp(199)] [Browser process] OnBeforeChildProcessLaunch() command line: "<>\Anaconda3\lib\site-packages\cefpython3\subprocess" --type=gpu-process --no-sandbox --locales-dir-path="C:\Users\Xiangyun\Anaconda3\lib\site-packages\cefpython3\locales" --log-file=debug.log --log-severity=info --resources-dir-path=" <>\Anaconda3\lib\site-packages\cefpython3" --lang=en-US --disable-gpu-shader-disk-cache --gpu-preferences=KAAAAAAAAACAA4CAAQAAAAAAAAAAAGAAEAAAAAAAAAAAAAAAAAAAACgAAAAEAAAAIAAAAAAAAAAoAAAAAAAAADAAAAAAAAAAOAAAAAAAAAAQAAAAAAAAAAAAAAAKAAAAEAAAAAAAAAAAAAAACwAAABAAAAAAAAAAAQAAAAoAAAAQAAAAAAAAAAEAAAALAAAA --locales-dir-path="<>\Anaconda3\lib\site-packages\cefpython3\locales" --log-file=debug.log --log-severity=info --resources-dir-path="<>\Anaconda3\lib\site-packages\cefpython3" --lang=en-US /prefetch:2
[0312/104311.473:INFO:cef_log.cpp(8)] [Browser process] CreateBrowserSync() called
DevTools listening on ws://127.0.0.1:59232/devtools/browser/abaff316-3b5c-4647-8af3-c7c521146d08
[0312/104311.473:INFO:cef_log.cpp(8)] [Browser process] navigateUrl: file://///<>/index_cefpython.html
[0312/104311.475:INFO:cef_log.cpp(8)] [Browser process] CefBrowser::CreateBrowserSync()
[0312/104311.493:INFO:cef_log.cpp(8)] [Browser process] GetPyBrowser(): create new PyBrowser, browserId=1
[0312/104311.516:INFO:cefpython_app.cpp(199)] [Browser process] OnBeforeChildProcessLaunch() command line: "<>\Anaconda3\lib\site-packages\cefpython3\subprocess" --type=renderer --no-sandbox --service-pipe-token=C31AF08C64F1883299C21D068EF5263C --lang=en-US --locales-dir-path=<>\Anaconda3\lib\site-packages\cefpython3\locales" --log-file=debug.log --log-severity=info --resources-dir-path="<>\Anaconda3\lib\site-packages\cefpython3" --disable-gpu-shader-disk-cache /prefetch:1
[0312/104311.528:INFO:cef_log.cpp(8)] [Browser process] GetPyFrame(): underlying frame does not yet exist: browserId = 1, frameId = -4
[0312/104311.529:INFO:cef_log.cpp(8)] [Browser process] GetPyFrame(): underlying frame does not yet exist: browserId = 1, frameId = -4
[0312/104311.531:INFO:cef_log.cpp(8)] [Browser process] CefBrowser::CreateBrowserSync() succeeded
[0312/104311.532:INFO:cef_log.cpp(8)] [Browser process] CefBrowser window handle = 595526
[0312/104311.533:INFO:cef_log.cpp(8)] [Browser process] GetPyFrame(): underlying frame does not yet exist: browserId = 1, frameId = -4
[0312/104311.534:INFO:cef_log.cpp(8)] [Browser process] SendProcessMessage(): message=DoJavascriptBindings, arguments size=1
[0312/104311.534:INFO:cef_log.cpp(8)] [Browser process] MessageLoop()
[0312/104311.612:INFO:client_handler.cpp(40)] [Browser process] OnProcessMessageReceived(): OnContextCreated
[0312/104311.613:INFO:cef_log.cpp(8)] [Browser process] V8ContextHandler_OnContextCreated()
[0312/104311.614:INFO:cef_log.cpp(8)] [Browser process] GetPyFrame(): create new PyFrame, frameId=2
Javascript bindinsgs 不应该用于传输大数据。要发送大数据,请改用 http 请求。基本上要么使用 AJAX 请求和 运行 一个内部应用程序内网络服务器,要么使用资源处理程序,它允许在没有 运行 网络服务器的情况下处理请求。
查看教程文档 > "Javascript integration" > "Communication using http requests" 部分(单击 link 后向下滚动一点):
https://github.com/cztomczak/cefpython/blob/master/docs/Tutorial.md#javascript-integration
您可以尝试使用 javacript 绑定以更小的部分发送数据,但使用请求的性能会更好。
检查您的应用内存在发送数据时的峰值情况。在 Linux 上使用进程监视器,在 Windows 上使用进程管理器。
要找出发送 200 MB 数据时失败的原因,请调试应用程序以获取堆栈跟踪。如果使用 Linux,则从标记为例如v66 上游。将 cefpython3 包中的原始 libcef.so 替换为那个。 GDB 命令可以在这里找到:
我正在尝试创建此 python 绑定到我创建的 electronjs 应用程序,其中 python 部分用于分析数据和结果(python 字典)被传递到网络浏览器进行可视化。对于少量数据,这一切都非常有效,但是当我尝试传递一个大的结果字典(~200MB)时,创建了 window,但在终端中没有警告就关闭了(我无法检查 devtool ),尽管后端似乎有一个 subprocess 保留 运行。 windows 和 Ubuntu 机器上都出现了这个问题,非常感谢你的帮助。
这是我的代码:
def view(data):
config = data
settings = {
"debug": True,
"log_severity": cef.LOGSEVERITY_INFO,
"log_file": "debug.log",
}
cef.Initialize(settings=settings)
browser_setting = { "file_access_from_file_urls_allowed":True,\
"universal_access_from_file_urls_allowed": True,\
"web_security_disabled":True}
browser = cef.CreateBrowserSync(url='file://' + os.path.realpath("index_cefpython.html"),
window_title="Javascript Bindings", settings = browser_setting)
browser.SetClientHandler(LoadHandler(config))
bindings = cef.JavascriptBindings()
browser.SetJavascriptBindings(bindings)
cef.MessageLoop()
del browser
cef.Shutdown()
return
class LoadHandler(object):
def __init__(self, config):
self.config = config
def OnLoadEnd(self, browser, **_):
browser.ExecuteFunction("defineData", self.config)
在 JS 方面,我有:
<script type="text/javascript">
function defineData(datainput){
console.log("start")
data = datainput;
Main();
}
</script>
这是终端中打印出的所有消息:
[0312/104311.439:INFO:cefpython_app.cpp(199)] [Browser process] OnBeforeChildProcessLaunch() command line: "<>\Anaconda3\lib\site-packages\cefpython3\subprocess" --type=gpu-process --no-sandbox --locales-dir-path="<>\Anaconda3\lib\site-packages\cefpython3\locales" --log-file=debug.log --log-severity=info --resources-dir-path="<>\Anaconda3\lib\site-packages\cefpython3" --lang=en-US --disable-gpu-shader-disk-cache /prefetch:2
[0312/104311.440:INFO:cef_log.cpp(8)] [Browser process] Switch already set, ignoring: disable-gpu-shader-disk-cache
[0312/104311.440:INFO:cefpython_app.cpp(199)] [Browser process] OnBeforeChildProcessLaunch() command line: "<>\Anaconda3\lib\site-packages\cefpython3\subprocess" --type=gpu-process --no-sandbox --locales-dir-path="C:\Users\Xiangyun\Anaconda3\lib\site-packages\cefpython3\locales" --log-file=debug.log --log-severity=info --resources-dir-path=" <>\Anaconda3\lib\site-packages\cefpython3" --lang=en-US --disable-gpu-shader-disk-cache --gpu-preferences=KAAAAAAAAACAA4CAAQAAAAAAAAAAAGAAEAAAAAAAAAAAAAAAAAAAACgAAAAEAAAAIAAAAAAAAAAoAAAAAAAAADAAAAAAAAAAOAAAAAAAAAAQAAAAAAAAAAAAAAAKAAAAEAAAAAAAAAAAAAAACwAAABAAAAAAAAAAAQAAAAoAAAAQAAAAAAAAAAEAAAALAAAA --locales-dir-path="<>\Anaconda3\lib\site-packages\cefpython3\locales" --log-file=debug.log --log-severity=info --resources-dir-path="<>\Anaconda3\lib\site-packages\cefpython3" --lang=en-US /prefetch:2
[0312/104311.473:INFO:cef_log.cpp(8)] [Browser process] CreateBrowserSync() called
DevTools listening on ws://127.0.0.1:59232/devtools/browser/abaff316-3b5c-4647-8af3-c7c521146d08 [0312/104311.473:INFO:cef_log.cpp(8)] [Browser process] navigateUrl: file://///<>/index_cefpython.html
[0312/104311.475:INFO:cef_log.cpp(8)] [Browser process] CefBrowser::CreateBrowserSync()
[0312/104311.493:INFO:cef_log.cpp(8)] [Browser process] GetPyBrowser(): create new PyBrowser, browserId=1
[0312/104311.516:INFO:cefpython_app.cpp(199)] [Browser process] OnBeforeChildProcessLaunch() command line: "<>\Anaconda3\lib\site-packages\cefpython3\subprocess" --type=renderer --no-sandbox --service-pipe-token=C31AF08C64F1883299C21D068EF5263C --lang=en-US --locales-dir-path=<>\Anaconda3\lib\site-packages\cefpython3\locales" --log-file=debug.log --log-severity=info --resources-dir-path="<>\Anaconda3\lib\site-packages\cefpython3" --disable-gpu-shader-disk-cache /prefetch:1
[0312/104311.528:INFO:cef_log.cpp(8)] [Browser process] GetPyFrame(): underlying frame does not yet exist: browserId = 1, frameId = -4
[0312/104311.529:INFO:cef_log.cpp(8)] [Browser process] GetPyFrame(): underlying frame does not yet exist: browserId = 1, frameId = -4
[0312/104311.531:INFO:cef_log.cpp(8)] [Browser process] CefBrowser::CreateBrowserSync() succeeded
[0312/104311.532:INFO:cef_log.cpp(8)] [Browser process] CefBrowser window handle = 595526
[0312/104311.533:INFO:cef_log.cpp(8)] [Browser process] GetPyFrame(): underlying frame does not yet exist: browserId = 1, frameId = -4
[0312/104311.534:INFO:cef_log.cpp(8)] [Browser process] SendProcessMessage(): message=DoJavascriptBindings, arguments size=1
[0312/104311.534:INFO:cef_log.cpp(8)] [Browser process] MessageLoop()
[0312/104311.612:INFO:client_handler.cpp(40)] [Browser process] OnProcessMessageReceived(): OnContextCreated
[0312/104311.613:INFO:cef_log.cpp(8)] [Browser process] V8ContextHandler_OnContextCreated()
[0312/104311.614:INFO:cef_log.cpp(8)] [Browser process] GetPyFrame(): create new PyFrame, frameId=2
Javascript bindinsgs 不应该用于传输大数据。要发送大数据,请改用 http 请求。基本上要么使用 AJAX 请求和 运行 一个内部应用程序内网络服务器,要么使用资源处理程序,它允许在没有 运行 网络服务器的情况下处理请求。
查看教程文档 > "Javascript integration" > "Communication using http requests" 部分(单击 link 后向下滚动一点):
https://github.com/cztomczak/cefpython/blob/master/docs/Tutorial.md#javascript-integration
您可以尝试使用 javacript 绑定以更小的部分发送数据,但使用请求的性能会更好。
检查您的应用内存在发送数据时的峰值情况。在 Linux 上使用进程监视器,在 Windows 上使用进程管理器。
要找出发送 200 MB 数据时失败的原因,请调试应用程序以获取堆栈跟踪。如果使用 Linux,则从标记为例如v66 上游。将 cefpython3 包中的原始 libcef.so 替换为那个。 GDB 命令可以在这里找到: