# Path: concert/config.py
# AIODEBUG = 9
# 
# Path: concert/coroutines/base.py
# def background(coroutine):
#     """Same as :func:`.start`, just meant to be used as a decorator."""
#     @functools.wraps(coroutine)
#     def inner(*args, **kwargs):
#         return start(coroutine(*args, **kwargs))
# 
#     return inner
# 
# def run_in_loop(coroutine):
#     """Wrap *coroutine* into a `asyncio.Task`, run it in the current loop, block until it finishes
#     and return the result.  On KeyboardInterrupt, the task is cancelled.
#     """
#     loop = asyncio.get_event_loop()
#     task = asyncio.ensure_future(coroutine, loop=loop)
# 
#     try:
#         result = loop.run_until_complete(task)
#         return result
#     except KeyboardInterrupt:
#         # _coro instead of get_coro() for Python 3.7 compatibilityget_coro()
#         LOG.log(AIODEBUG, "KeyboardInterrupt in `%s', cancelling", task._coro.__qualname__)
#         task.cancel()
# 
# Path: concert/devices/base.py
# class Device(Parameterizable):
# 
#     """
#     A :class:`.Device` provides locked access to a real-world device.
# 
#     It implements the context protocol to provide locking::
# 
#         async with device:
#             # device is locked
#             await device.set_parameter(1 * q.m)
#             ...
# 
#         # device is unlocked again
#     """
# 
#     def __init__(self):
#         # We have to create the lock early on because it will be accessed in
#         # any add_parameter calls, especially those in the Parameterizable base
#         # class
#         self._lock = asyncio.Lock()
#         super(Device, self).__init__()
# 
#     async def __aenter__(self):
#         await self._lock.acquire()
# 
#         return self
# 
#     async def __aexit__(self, exc_type, exc, tb):
#         self._lock.release()
# 
#     @background
#     async def emergency_stop(self):
#         """Emergency stop."""
#         await self._emergency_stop()
# 
#     async def _emergency_stop(self):
#         """Emergency stop implementation."""
#         pass
# 
# Path: concert/quantities.py



import asyncio
import logging
import math
import os
import inspect
import subprocess
import prettytable
    import concert.coroutines.base as cbase
    import functools
    import threading
from concert.config import AIODEBUG
from concert.coroutines.base import background, run_in_loop
from concert.devices.base import Device
from concert.quantities import q
    from concert.devices.base import Device
        from pygments import highlight
        from pygments.lexers import PythonLexer
        from pygments.formatters import TerminalFormatter
        emergency_stops = []
        emergency_device_names = []
        for name, value in uns.items():
            if not name.startswith('_') and isinstance(value, Device):
                # ipython adds _1, _2, ... variables to user_ns when people type variable names in
                # the shell
                LOG.info("Emergency stop on `%s'", name)
                emergency_stops.append(value.emergency_stop())
                emergency_device_names.append(name)

    return False


@background
async def check_emergency_stop(check, poll_interval=0.1 * q.s, exit_session=False):
    """
    check_emergency_stop(check, poll_interval=0.1*q.s, exit_session=False)
    If a callable *check* returns True abort is called. Then until it clears to False nothing is
    done and then the process begins again. *poll_interval* is the interval at which *check* is
    called. If *exit_session* is True the session exits when the emergency stop occurs.
    """
    while True:
        if check():
            LOG.error('Emergency stop')
            abort_awaiting(background=True, skip='check_emergency_stop')
            if exit_session:
                os.abort()
            while check():
                # Wait until the flag clears
                await asyncio.sleep(poll_interval.to(q.s).magnitude)

# Path: concert/config.py
# AIODEBUG = 9
# 
# Path: concert/coroutines/base.py
# def background(coroutine):
#     """Same as :func:`.start`, just meant to be used as a decorator."""
#     @functools.wraps(coroutine)
#     def inner(*args, **kwargs):
#         return start(coroutine(*args, **kwargs))
# 
#     return inner
# 
# def run_in_loop(coroutine):
#     """Wrap *coroutine* into a `asyncio.Task`, run it in the current loop, block until it finishes
#     and return the result.  On KeyboardInterrupt, the task is cancelled.
#     """
#     loop = asyncio.get_event_loop()
#     task = asyncio.ensure_future(coroutine, loop=loop)
# 
#     try:
#         result = loop.run_until_complete(task)
#         return result
#     except KeyboardInterrupt:
#         # _coro instead of get_coro() for Python 3.7 compatibilityget_coro()
#         LOG.log(AIODEBUG, "KeyboardInterrupt in `%s', cancelling", task._coro.__qualname__)
#         task.cancel()
# 
# Path: concert/devices/base.py
# class Device(Parameterizable):
# 
#     """
#     A :class:`.Device` provides locked access to a real-world device.
# 
#     It implements the context protocol to provide locking::
# 
#         async with device:
#             # device is locked
#             await device.set_parameter(1 * q.m)
#             ...
# 
#         # device is unlocked again
#     """
# 
#     def __init__(self):
#         # We have to create the lock early on because it will be accessed in
#         # any add_parameter calls, especially those in the Parameterizable base
#         # class
#         self._lock = asyncio.Lock()
#         super(Device, self).__init__()
# 
#     async def __aenter__(self):
#         await self._lock.acquire()
# 
#         return self
# 
#     async def __aexit__(self, exc_type, exc, tb):
#         self._lock.release()
# 
#     @background
#     async def emergency_stop(self):
#         """Emergency stop."""
#         await self._emergency_stop()
# 
#     async def _emergency_stop(self):
#         """Emergency stop implementation."""
#         pass
# 
# Path: concert/quantities.py



# Path: concert/session/utils.py
import asyncio
import logging
import math
import os
import inspect
import subprocess
import prettytable
    import concert.coroutines.base as cbase
    import functools
    import threading
from concert.config import AIODEBUG
from concert.coroutines.base import background, run_in_loop
from concert.devices.base import Device
from concert.quantities import q
    from concert.devices.base import Device
        from pygments import highlight
        from pygments.lexers import PythonLexer
        from pygments.formatters import TerminalFormatter

        emergency_stops = []
        emergency_device_names = []
        for name, value in uns.items():
            if not name.startswith('_') and isinstance(value, Device):
                # ipython adds _1, _2, ... variables to user_ns when people type variable names in
                # the shell
                LOG.info("Emergency stop on `%s'", name)
                emergency_stops.append(value.emergency_stop())
                emergency_device_names.append(name)

    return False


@background
async def check_emergency_stop(check, poll_interval=0.1 * q.s, exit_session=False):
    """
    check_emergency_stop(check, poll_interval=0.1*q.s, exit_session=False)
    If a callable *check* returns True abort is called. Then until it clears to False nothing is
    done and then the process begins again. *poll_interval* is the interval at which *check* is
    called. If *exit_session* is True the session exits when the emergency stop occurs.
    """
    while True:
        if check():
            LOG.error('Emergency stop')
            abort_awaiting(background=True, skip='check_emergency_stop')
            if exit_session:
                os.abort()
            while check():
                # Wait until the flag clears
                await asyncio.sleep(poll_interval.to(q.s).magnitude)

        await asyncio.sleep(poll_interval.to(q.s).magnitude)