locking before update #340
-
I'm using a persistent variable pyscript.counter. from threading import Lock lock.acquire() I tried to implement that, but, evertime HA got unresponsive and had to turn my RPI off and on again. OR is there another way, a better way? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Yes, there will be a race condition and it's great that you are aware of the issue. You must use an async lock, not a thread lock. Try this: import asyncio
counter_lock = asyncio.Lock()
def my_function():
counter_lock.acquire()
counter += 1
counter_lock.release() (note: untested) |
Beta Was this translation helpful? Give feedback.
-
Wow, thanks
Will try that!
Op wo 13 apr. 2022 20:10 schreef Craig Barratt ***@***.***>:
… Yes, there will be a race condition and it's great that you are aware of
the issue.
You must use an async lock, not a thread lock. Try this:
import asyncio
counter_lock = asyncio.Lock()
def my_function():
counter_lock.acquire()
counter += 1
counter_lock.release()
(note: untested)
—
Reply to this email directly, view it on GitHub
<#340 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACV5I2CYDVCO6M6PUBPZKVTVE4EXTANCNFSM5TKC3J2A>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
com>
|
Beta Was this translation helpful? Give feedback.
Yes, there will be a race condition and it's great that you are aware of the issue.
You must use an async lock, not a thread lock. Try this:
(note: untested)