|
9 | 9 | import platform |
10 | 10 | import sys |
11 | 11 | import time |
| 12 | +import traceback |
12 | 13 |
|
13 | 14 | import win32api |
14 | 15 | import win32con |
|
19 | 20 | # Platforms (Windows only) |
20 | 21 | assert(platform.system() == "Windows") |
21 | 22 |
|
22 | | -def main(): |
| 23 | +def main(multi_threaded_message_loop): |
| 24 | + |
23 | 25 | check_versions() |
24 | 26 | sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error |
25 | | - cef.Initialize() |
26 | | - pyWin32Example() |
27 | | - cef.Shutdown() |
28 | | - |
29 | | - |
30 | | -def check_versions(): |
31 | | - print("[pywin32.py] CEF Python {ver}".format(ver=cef.__version__)) |
32 | | - print("[pywin32.py] Python {ver} {arch}".format(ver=platform.python_version(), arch=platform.architecture()[0])) |
33 | | - print("[pywin32.py] pywin32 {ver}".format(ver=GetPywin32Version())) |
34 | | - assert cef.__version__ >= "55.3", "CEF Python v55.3+ required to run this" |
35 | | - |
36 | | - |
37 | | -def pyWin32Example(): |
38 | 27 |
|
39 | | - cef.Initialize() |
40 | | - |
| 28 | + settings = {"multi_threaded_message_loop": 1 if multi_threaded_message_loop else 0} |
| 29 | + cef.Initialize(settings) |
| 30 | + |
41 | 31 | wndproc = { |
42 | 32 | win32con.WM_CLOSE: CloseWindow, |
43 | 33 | win32con.WM_DESTROY: QuitApplication, |
44 | 34 | win32con.WM_SIZE: WindowUtils.OnSize, |
45 | 35 | win32con.WM_SETFOCUS: WindowUtils.OnSetFocus, |
46 | 36 | win32con.WM_ERASEBKGND: WindowUtils.OnEraseBackground |
47 | 37 | } |
48 | | - |
49 | 38 | windowHandle = CreateWindow(title="pywin32 example", className="cefpython3_example", width=1024, height=768, windowProc=wndproc) |
50 | 39 |
|
51 | 40 | windowInfo = cef.WindowInfo() |
52 | 41 | windowInfo.SetAsChild(windowHandle) |
| 42 | + |
| 43 | + if(multi_threaded_message_loop): |
| 44 | + # when using multi-threaded message loop, CEF's UI thread is no more application's main thread |
| 45 | + cef.PostTask(cef.TID_UI, _createBrowserInUiThread, windowInfo, {}, "https://www.google.com/") |
| 46 | + win32gui.PumpMessages() |
| 47 | + |
| 48 | + else: |
| 49 | + browser = _createBrowserInUiThread(windowInfo, {}, "https://www.google.com/") |
| 50 | + cef.MessageLoop() |
| 51 | + |
| 52 | + cef.Shutdown() |
| 53 | + |
| 54 | + |
| 55 | +def check_versions(): |
| 56 | + print("[pywin32.py] CEF Python {ver}".format(ver=cef.__version__)) |
| 57 | + print("[pywin32.py] Python {ver} {arch}".format(ver=platform.python_version(), arch=platform.architecture()[0])) |
| 58 | + print("[pywin32.py] pywin32 {ver}".format(ver=GetPywin32Version())) |
| 59 | + assert cef.__version__ >= "55.3", "CEF Python v55.3+ required to run this" |
| 60 | + |
| 61 | + |
| 62 | +def _createBrowserInUiThread(windowInfo, settings, url): |
| 63 | + |
| 64 | + assert(cef.IsThread(cef.TID_UI)) |
53 | 65 | browser = cef.CreateBrowserSync(windowInfo, settings={}, |
54 | 66 | url="https://www.google.com/") |
55 | | - cef.MessageLoop() |
56 | | - cef.Shutdown() |
57 | 67 |
|
58 | 68 |
|
59 | 69 | def CloseWindow(windowHandle, message, wparam, lparam): |
@@ -104,4 +114,12 @@ def GetPywin32Version(): |
104 | 114 |
|
105 | 115 |
|
106 | 116 | if __name__ == '__main__': |
107 | | - main() |
| 117 | + |
| 118 | + if "--multi_threaded_message_loop" in sys.argv: |
| 119 | + print("[pywin32.py] Message loop mode: CEF multi-threaded (best performance)") |
| 120 | + multi_threaded_message_loop = True |
| 121 | + else: |
| 122 | + print("[pywin32.py] Message loop mode: CEF single-threaded") |
| 123 | + multi_threaded_message_loop = False |
| 124 | + |
| 125 | + main(multi_threaded_message_loop) |
0 commit comments