Answer the question
In order to leave comments, you need to log in
Why does a thread in Python run asynchronously?
Hey!
I have a little gag with Python threads.
I'm using the normal threading module to run a function as a "daemon". when starting the Flask server.
The function itself has logic inside it and a number of functions called by this logic mixed with each other.
It turns out that in the log I see a wild mixture of asynchronous function launches of all the logic that I registered.
Having tested running my function separately in parts and together without entering into threading, everything works fine.
My function does something like this (BE CAREFUL PSEUDOCODE):
def start()
if "файл есть" == 'файл есть':
while True :
записываем данные в файл с последней точки (вызываем функцию b1).
else:
создаем файл (вызываем функцию a1)
наполняем файл данными (вызываем функцию a2)
логика проверки даты и времени в записях и т.д. (вызываем функцию a3)
while True :
бесконечный цикл наполнения файла последней информацией.(вызываем функцию b1)
flow1 = Thread(target=start, daemon=True)
flow1.start()
Answer the question
In order to leave comments, you need to log in
The solution is the following. Found the answer in What is the best way to organize two threads of execution inside Flask?
The problem is that Flask already has a flow, and because of this, such parsley as mine happens. It turns out I already have 2 threads and Flask is already somehow spinning them.
Solution: Manually make a separate run() thread and your own
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('index.html')
flow1 = Thread(target=app.run)
flow2 = Thread(target=momowwind, daemon=True)
flow1.start()
flow2.start()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question