Search code examples
pythonpython-3.xwebserverpython-multithreadingchromium-embedded

Program paused while thread is being executed


I have a program that should start a web server (as the thread), and then display it in a CEF Browser (not a thread). But when I start it, it just waits for the thread to stop executing, which it will never do, since its an infinite loop.

print("Server started http://%s:%s" % (hostName, serverPort))
webServerThread = threading.Thread(target=os.system, args = ("python -m http.server 8081", ), daemon=True)
webServerThread.start()
webServerThread.join()
print('Initialising CEF')
cef.Initialize()
print('[DEBUG] CreateBrowserSync')
cef.CreateBrowserSync(url="http://127.0.0.1:8081/compfile.html")
print('[DEBUG] MessageLoop')
cef.MessageLoop()

I have tried to search on stackoverflow and google and I didn't find anything that would help me.


Solution

  • When you run webServerThread.join() immediately after webServerThread.start(), your app still have a run flow like a single-thread application. Move webServerThread.join() to the end.

    print("Server started http://%s:%s" % (hostName, serverPort))
    webServerThread = threading.Thread(target=os.system, args = ("python -m http.server 8081", ), daemon=True)
    webServerThread.start()
    
    print('Initialising CEF')
    cef.Initialize()
    print('[DEBUG] CreateBrowserSync')
    cef.CreateBrowserSync(url="http://127.0.0.1:8081/compfile.html")
    print('[DEBUG] MessageLoop')
    cef.MessageLoop()
    
    webServerThread.join()