reactive.lock

reactive.lock()

Deprecated. Set a reactive value directly instead.

Apart from emitting a deprecation warning (once per process), this function does nothing. It returns an asyncio.Lock that never blocks, so holding it neither pauses reactive processing nor keeps out other code that holds it. Code that needs mutual exclusion of its own should create its own asyncio.Lock.

To change reactive state from outside a reactive context (for example, from a background asyncio.Task), set the value directly. Setting it schedules a round, so there's no need to call flush afterwards:

# Deprecated
async with reactive.lock():
    current_query.set(query)
    await reactive.flush()

# Use instead
current_query.set(query)

Note that the deprecated form returned only once the dependents had run, while set() returns before they run. Keep the await reactive.flush() (without the lock) if the code after it relies on that.

Effects of a session that are paused at an await see the new value as soon as it is set. To apply the change only once the session's effects have finished, as Shiny does for input changes from the client, use run_once_when_idle:

session.run_once_when_idle(lambda: current_query.set(query))