function async_execute
Wraps and schedules async function execution in the appropriate event loop context, ensuring proper lock propagation and document context management for Bokeh/Panel applications.
/tf/active/vicechatdev/patches/server.py
85 - 110
complex
Purpose
This function handles the execution of async functions in both standalone and Bokeh server contexts. It manages event loop scheduling, propagates locking flags from wrapped functions, and ensures the correct document context is maintained during execution. It's primarily used in Panel/Bokeh applications to safely execute async callbacks while respecting concurrency controls.
Source Code
def async_execute(func):
"""
Wrap async event loop scheduling to ensure that with_lock flag
is propagated from function to partial wrapping it.
"""
if not state.curdoc or not state.curdoc.session_context:
ioloop = IOLoop.current()
event_loop = ioloop.asyncio_loop
if event_loop.is_running():
ioloop.add_callback(func)
else:
event_loop.run_until_complete(func())
return
if isinstance(func, partial) and hasattr(func.func, 'lock'):
unlock = not func.func.lock
else:
unlock = not getattr(func, 'lock', False)
curdoc = state.curdoc
@wraps(func)
async def wrapper(*args, **kw):
with set_curdoc(curdoc):
return await func(*args, **kw)
if unlock:
wrapper.nolock = True
state.curdoc.add_next_tick_callback(wrapper)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
func |
- | - | positional_or_keyword |
Parameter Details
func: An async function or partial function to be executed. Can have a 'lock' attribute that controls whether the function should be executed with or without document locking. If func is a functools.partial, the lock attribute is checked on the underlying function.
Return Value
Returns None. The function schedules the provided async function for execution but does not return its result. In non-document contexts, it either adds the function as a callback to the IOLoop or runs it to completion depending on whether the event loop is running.
Dependencies
tornadobokehfunctools
Required Imports
from tornado.ioloop import IOLoop
from functools import partial
from functools import wraps
from bokeh.io.doc import set_curdoc
Conditional/Optional Imports
These imports are only needed under specific conditions:
from state import state
Condition: Required for accessing the current document (curdoc) and session context
Required (conditional)from bokeh.io.doc import set_curdoc
Condition: Required for setting document context in the wrapper function
Required (conditional)Usage Example
import asyncio
from functools import partial
from tornado.ioloop import IOLoop
# Example 1: Simple async function
async def my_async_task():
await asyncio.sleep(1)
print('Task completed')
async_execute(my_async_task)
# Example 2: Async function with lock attribute
async def locked_task():
await asyncio.sleep(1)
print('Locked task completed')
locked_task.lock = True
async_execute(locked_task)
# Example 3: Partial function with lock
async def parameterized_task(value):
await asyncio.sleep(1)
print(f'Value: {value}')
parameterized_task.lock = False
partial_func = partial(parameterized_task, value=42)
async_execute(partial_func)
# Note: This function is typically used within Panel/Bokeh applications
# where state.curdoc is automatically set
Best Practices
- Ensure the 'state' module is properly initialized before calling this function
- Use the 'lock' attribute on functions to control document locking behavior in Bokeh server contexts
- When using partial functions, set the lock attribute on the original function, not the partial
- This function is designed for Panel/Bokeh applications and may not work correctly in other async contexts
- The function handles two distinct execution paths: one for Bokeh document contexts and one for standalone event loops
- Do not expect a return value from this function; it schedules execution but does not wait for completion
- Ensure an IOLoop is running when using this function outside of a Bokeh document context
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function with_lock 63.3% similar
-
function unlocked 62.8% similar
-
function _eval_panel 56.1% similar
-
function modify_document 54.3% similar
-
class Application 54.0% similar