bluesky_queueserver_api.WaitMonitor

class bluesky_queueserver_api.WaitMonitor[source]

Creates monitor object for wait_... operations, such as wait_for_idle. The object may be used to stop the operation from another thread or asynchronous task.

Examples

The examples illustrate how to use WaitMonitor object to cancel wait operations. Synchronous code (0MQ or HTTP):

# Synchronous code
from bluesky_queueserver_api import Wait Monitor
from bluesky_queueserver_api.zmq import REManagerAPI()  # Same for HTTP
import time
import threading

RM = REManagerAPI()
monitor = WaitMonitor()

def wait_re_manager_idle():
    try:
        print("Waiting ...")
        RM.wait_for_idle(monitor=monitor)
    except RM.WaitCancelError:
        print("Cancelled.")
    except RM.WaitTimeoutError:
        print("Timeout.")
    print("RE Manager is idle")

# Wait until RE Manager is in 'idle' state in a background thread
thread = threading.Thread(target=wait_re_manager_idle)
thread.start()
# Cancel wait after 2 seconds from main thread
time.sleep(2)
monitor.cancel()

thread.join()
RM.close()

Asynchronous code example (0MQ or HTTP):

# Asynchronous code
import asyncio
from bluesky_queueserver_api import Wait Monitor
from bluesky_queueserver_api.zmq.aio import REManagerAPI()  # Same for HTTP
import time

async def testing():

    RM = REManagerAPI()
    monitor = WaitMonitor()

    async def wait_re_manager_idle():
        try:
            print("Waiting ...")
            await RM.wait_for_idle(monitor=monitor)
        except RM.WaitCancelError:
            print("Cancelled.")
        except RM.WaitTimeoutError:
            print("Timeout.")
        print("RE Manager is idle")

    # Wait until RE Manager is in 'idle' state in a background task
    asyncio.create_task(wait_re_manager_idle())
    # Cancel wait after 2 seconds from main thread
    await asyncio.sleep(2)
    monitor.cancel()
    await asyncio.sleep(0.5)  # Let the task to complete
    await RM.close()

asyncio.run(testing())
__init__()[source]

Methods

__init__()

add_cancel_callback(cancel_callback)

Each callbacks is called only once before the operation is cancelled.

cancel()

Cancel the currently running operation.

set_timeout(timeout)

Modify timeout for the current operation (seconds).

Attributes

is_cancelled

Checks if the monitor was cancelled.

time_elapsed

Time since the operation started (seconds).

time_start

Time when the operation started (seconds).

timeout

Timeout (seconds).