Answer the question
In order to leave comments, you need to log in
Pause loop execution of one function from another?
There are two asynchronous functions, the lunch loops while True.
1 function reads a variable from the base in a loop every 5 minutes and then works with it.
2 function changes this variable and writes to the database every 2 minutes
There is a possibility that 1 function will receive the variable and the second will change it before the loop in the first one
ends
async def fun_1():
while True:
sql = "SELECT variable FROM table_variable WHERE ids = '1'"
variable_X = cursor.execute(sql).fetchall()[0][0]
variable_Y = variable_X + variable_Z
await asyncio.sleep(5)
variable_W = variable_X + variable_Z
await asyncio.sleep(5)
variable_F = variable_X + variable_Z
await asyncio.sleep(300)
async def fun_2():
while True:
variable_X = # Some code
# ЗАПИСЬ В ТАБЛИЦУ table_variable
await asyncio.sleep(180)
Answer the question
In order to leave comments, you need to log in
Everything is bad here.
1. stop using global variables
2. what's the point of using async when working with the database is blocking?
And so, of course, there is a mutex in the form of asyncio.Lock, but damn, this code is an example of how to do everything wrong.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question