Skip to content

Commit d893abe

Browse files
committed
Implementing 'multi_threaded_message_loop' in pywin32 example
1 parent be95517 commit d893abe

1 file changed

Lines changed: 38 additions & 20 deletions

File tree

examples/pywin32.py

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import platform
1010
import sys
1111
import time
12+
import traceback
1213

1314
import win32api
1415
import win32con
@@ -19,41 +20,50 @@
1920
# Platforms (Windows only)
2021
assert(platform.system() == "Windows")
2122

22-
def main():
23+
def main(multi_threaded_message_loop):
24+
2325
check_versions()
2426
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():
3827

39-
cef.Initialize()
40-
28+
settings = {"multi_threaded_message_loop": 1 if multi_threaded_message_loop else 0}
29+
cef.Initialize(settings)
30+
4131
wndproc = {
4232
win32con.WM_CLOSE: CloseWindow,
4333
win32con.WM_DESTROY: QuitApplication,
4434
win32con.WM_SIZE: WindowUtils.OnSize,
4535
win32con.WM_SETFOCUS: WindowUtils.OnSetFocus,
4636
win32con.WM_ERASEBKGND: WindowUtils.OnEraseBackground
4737
}
48-
4938
windowHandle = CreateWindow(title="pywin32 example", className="cefpython3_example", width=1024, height=768, windowProc=wndproc)
5039

5140
windowInfo = cef.WindowInfo()
5241
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))
5365
browser = cef.CreateBrowserSync(windowInfo, settings={},
5466
url="https://www.google.com/")
55-
cef.MessageLoop()
56-
cef.Shutdown()
5767

5868

5969
def CloseWindow(windowHandle, message, wparam, lparam):
@@ -104,4 +114,12 @@ def GetPywin32Version():
104114

105115

106116
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

Comments
 (0)