M
M
Morrowind2021-07-05 14:55:58
Flask
Morrowind, 2021-07-05 14:55:58

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


Or

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


My guess is that he doesn't like my threads in Flask.

Example 1:
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()


+ to this, the web-muzzle ceases to open at any of the addresses that you can think of.

The option where I manually prescribe ports and ip gives the effect of the web-muzzle, but alas, flow2 does not start automatically. Even on the local computer. The server just starts, the web face and it doesn’t reach the demon in flow2.
When launched from the container console, it also does not start with the same errors as above.

Example2 :
flow1 = Thread(target=app.run(host='0.0.0.0', port=5000, debug=True))
flow2 = Thread(target=returnTradeHistory, daemon=True)

flow1.start()
flow2.start()


Question: I don't know where exactly to dig.
Is it a matter of the Debian container that forms docker-compose and the streams won't take off from it?
Or some other reason?

Removing streams is not an option. I need a demon, and I don't have much time to fight Celery .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Morrowind, 2021-07-16
@hekkaaa

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.

P
Philip Bondarev, 2021-07-15
@Dogrtt

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 question

Ask a Question

731 491 924 answers to any question