# Path: concert/config.py
# AIODEBUG = 9
# 
# Path: concert/helpers.py
# class PrioItem:
# 
#     """To be used in combination with `queue.PriorityQueue`."""
# 
#     priority: int
#     data: Any = field(compare=False)
# 
# Path: concert/quantities.py



import asyncio
import concert.config
import concurrent.futures
import functools
import queue
import logging
import time
from threading import Thread
from concert.config import AIODEBUG
from concert.helpers import PrioItem
from concert.quantities import q
    :class:`concert.helpers.PrioItem`.
    """
    loop = asyncio.get_running_loop()
    pqueue = queue.PriorityQueue()

    try:
        future = loop.run_in_executor(None, func, pqueue, *args)
        prio = 1
        async for item in producer:
            pqueue.put(PrioItem(priority=prio, data=item))
            prio += 1
    except (asyncio.CancelledError, KeyboardInterrupt) as e:
        LOG.log(concert.config.AIODEBUG, f'feed_queue cancelled by exception {type(e)}')
        # Highest priority, effectively cancel processing of everything in the queue
        prio = 0
        raise
    finally:
        LOG.log(concert.config.AIODEBUG, f'feed_queue finished with priority {prio}')
        pqueue.put(PrioItem(priority=prio, data=None))
        await future


async def wait_until(condition, sleep_time=1e-1 * q.s, timeout=None):
    """Wait until a callable *condition* returns True. *sleep_time* is the time to sleep
    between consecutive checks of *condition*. If *timeout* is given and the *condition* doesn't
    return True within the time specified by it a :class:`.WaitingError` is raised.
    """
    sleep_time = sleep_time.to(q.s).magnitude
    if timeout:
        start = time.time()

# Path: concert/config.py
# AIODEBUG = 9
# 
# Path: concert/helpers.py
# class PrioItem:
# 
#     """To be used in combination with `queue.PriorityQueue`."""
# 
#     priority: int
#     data: Any = field(compare=False)
# 
# Path: concert/quantities.py



# Path: concert/coroutines/base.py
import asyncio
import concert.config
import concurrent.futures
import functools
import queue
import logging
import time
from threading import Thread
from concert.config import AIODEBUG
from concert.helpers import PrioItem
from concert.quantities import q

    :class:`concert.helpers.PrioItem`.
    """
    loop = asyncio.get_running_loop()
    pqueue = queue.PriorityQueue()

    try:
        future = loop.run_in_executor(None, func, pqueue, *args)
        prio = 1
        async for item in producer:
            pqueue.put(PrioItem(priority=prio, data=item))
            prio += 1
    except (asyncio.CancelledError, KeyboardInterrupt) as e:
        LOG.log(concert.config.AIODEBUG, f'feed_queue cancelled by exception {type(e)}')
        # Highest priority, effectively cancel processing of everything in the queue
        prio = 0
        raise
    finally:
        LOG.log(concert.config.AIODEBUG, f'feed_queue finished with priority {prio}')
        pqueue.put(PrioItem(priority=prio, data=None))
        await future


async def wait_until(condition, sleep_time=1e-1 * q.s, timeout=None):
    """Wait until a callable *condition* returns True. *sleep_time* is the time to sleep
    between consecutive checks of *condition*. If *timeout* is given and the *condition* doesn't
    return True within the time specified by it a :class:`.WaitingError` is raised.
    """
    sleep_time = sleep_time.to(q.s).magnitude
    if timeout:
        start = time.time()

        timeout = timeout.to(q.s).magnitude