Reputation: 35
I launch a simple TCP server listening on say, port 6677 in a thread separate from the main thread using threading.start_new_thread()
. Basically all this server does is:
sock = socket(AF_INET, SOCK_STREAM)
sock.bind(('', 6677))
sock.listen(5)
...
print 'Listening...'
while True:
conn, addr = sock.accept()
print 'connected!'
...
After launching that server in the separate thread, I run a simple Flask application in the main thread:
app.run()
Which defaults to use port 5000. The Flask app runs fine, but causes an [Errno 98] Address already in use in the bind() call in the other thread! This is after "Listening ..." has been printed from the secondary thread. What black magic is going on here? Can two servers not listen on different ports on the same address?
Upvotes: 2
Views: 1683
Reputation: 100766
Do you call app.run(debug=True)
?
If so, the Flask automatic reloading system will start your process, then kill it, then start it again (I believe).
Do app.run(debug=True, use_reloader=False)
instead.
Upvotes: 2