Reputation: 111
I'm trying to run a WebServer and socket server in Python. I have the code for each that works on it's own, but I can't get them going together and they each start on a blocking call - can someone point out my ?newbie? mistake(s) below?
start_server = websockets.serve(hello, "", 8765)
print("one")
import WWW_Server
#The imported file has the following lines:
##from waitress import serve
##
##def srvThread(SrvPort=5000):
## flaskThread()
## serve(app, host='0.0.0.0', port=SrvPort)
##
##threading.Thread(target = srvThread(2000)).start()
##print("hello") <<<< Why don't I see this printed out?
print("two") # <<<< Why don't I see this printed out?
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
print("three")
Upvotes: 0
Views: 146
Reputation: 468
You don't see 'hello' or 'two' printed out because you don't get past the threading.Thread(target = srvThread(2000)).start()
line in your import. That kicks off a blocking thread (that's what .start()
does), and it just hangs there, never getting back to your main thread.
To move forward with this, I suggest using python's multiprocessing library, which allows for true parallelism.
Additionally, the way your code is written is going to lead you down a path of frustration and angst - generally speaking, you never want your imports to do thing independently of your main. It leads to confusion and spaghetti code, and very frustrating debug stacks. Ideally, your imports are like libraries. They set up code for you to call from your main.
Upvotes: 1