Answer the question
In order to leave comments, you need to log in
How to correctly configure Docker compose - threads in Flask server?
Hello.
I've been fiddling with docker compose for 2 days now.
The problem is that the container does not start correctly.
On a local computer my scheme works. (not surprising)
In docker, I run test functions that access the database - everything is ok.
Problem starting main.py.
When starting the container with the Flask server, I catch the following:
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.9/threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "/server/start.py", line 20, in returnTradeHistory
check_table_db = chectablefromDB(name_table)
File "/server/DataFormatCorrection/UpdateData.py", line 203, in chectablefromDB
if connection:
UnboundLocalError: local variable 'connection' referenced before assignment
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.9/threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 922, in run
run_simple(t.cast(str, host), port, self, **options)
File "/usr/local/lib/python3.9/site-packages/werkzeug/serving.py", line 1008, in run_simple
inner()
File "/usr/local/lib/python3.9/site-packages/werkzeug/serving.py", line 948, in inner
srv = make_server(
File "/usr/local/lib/python3.9/site-packages/werkzeug/serving.py", line 780, in make_server
return ThreadedWSGIServer(
File "/usr/local/lib/python3.9/site-packages/werkzeug/serving.py", line 686, in __init__
super().__init__(server_address, handler) # type: ignore
File "/usr/local/lib/python3.9/socketserver.py", line 452, in __init__
self.server_bind()
File "/usr/local/lib/python3.9/http/server.py", line 138, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/local/lib/python3.9/socketserver.py", line 466, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('index.html')
flow1 = Thread(target=app.run)
flow2 = Thread(target=returnTradeHistory, daemon=True)
flow1.start()
flow2.start()
flow1 = Thread(target=app.run(host='0.0.0.0', port=5000, debug=True))
flow2 = Thread(target=returnTradeHistory, daemon=True)
flow1.start()
flow2.start()
Answer the question
In order to leave comments, you need to log in
Problem solved.
Prompted the decision shurshur
It was necessary to define a variable connection = None in functions using connect.
I cached the problem of starting threads by specifying in the dockerfile of the server assembly instead of RUN "python", "main" - RUN "flask","main".
When starting through python, it occupied the interpreter completely and did not allow the daemon to start.
It will work with 2-3 demons, time will tell.
Collect a berry - you will collect a box.
You'll end up spending more time on "costlying," I guarantee.
Sit down, deal with celery, there is absolutely nothing complicated. But there is confidence that everything works and in which case you can find articles / ask for advice from those who have already worked with him.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question