Python asyncio.Queue() Examples

The following are 30 code examples of asyncio.Queue(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module asyncio , or try the search function .
Example #1
Source File: duofern_stick.py    From pyduofern with GNU General Public License v2.0 7 votes vote down vote up
def __init__(self, loop=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.duofern_parser.asyncio = True
        self.initialization_step = 0
        self.loop = loop
        self.write_queue = asyncio.Queue()
        self._ready = asyncio.Event()
        self.transport = None
        self.buffer = bytearray(b'')

        self.last_packet = 0.0
        self.callback = None

        if loop == None:
            loop = asyncio.get_event_loop()

        self.send_loop = asyncio.ensure_future(self._send_messages(), loop=loop)

        self.available = asyncio.Future()

        # DuofernStick.__init__(self, device, system_code, config_file_json, duofern_parser)

    #        self.serial_connection = serial.Serial(self.port, baudrate=115200, timeout=1)
    #        self.running = False 
Example #2
Source File: broadcast.py    From quart with MIT License 7 votes vote down vote up
def sse():
    queue = asyncio.Queue()
    app.clients.add(queue)
    async def send_events():
        while True:
            try:
                data = await queue.get()
                event = ServerSentEvent(data)
                yield event.encode()
            except asyncio.CancelledError as error:
                app.clients.remove(queue)

    response = await make_response(
        send_events(),
        {
            'Content-Type': 'text/event-stream',
            'Cache-Control': 'no-cache',
            'Transfer-Encoding': 'chunked',
        },
    )
    response.timeout = None
    return response 
Example #3
Source File: help_channels.py    From bot with MIT License 7 votes vote down vote up
def __init__(self, bot: Bot):
        super().__init__()

        self.bot = bot

        # Categories
        self.available_category: discord.CategoryChannel = None
        self.in_use_category: discord.CategoryChannel = None
        self.dormant_category: discord.CategoryChannel = None

        # Queues
        self.channel_queue: asyncio.Queue[discord.TextChannel] = None
        self.name_queue: t.Deque[str] = None

        self.name_positions = self.get_names()
        self.last_notification: t.Optional[datetime] = None

        # Asyncio stuff
        self.queue_tasks: t.List[asyncio.Task] = []
        self.ready = asyncio.Event()
        self.on_message_lock = asyncio.Lock()
        self.init_task = self.bot.loop.create_task(self.init_cog()) 
Example #4
Source File: test_local.py    From quart with MIT License 6 votes vote down vote up
def test_task_local() -> None:
    local_ = TaskLocal()
    queue: asyncio.Queue = asyncio.Queue()
    tasks = 2
    for _ in range(tasks):
        queue.put_nowait(None)

    async def _test_local(value: int) -> int:
        local_.test = value
        await queue.get()
        queue.task_done()
        await queue.join()
        return local_.test

    futures = [asyncio.ensure_future(_test_local(value)) for value in range(tasks)]
    asyncio.gather(*futures)
    for value, future in enumerate(futures):
        assert (await future) == value 
Example #5
Source File: telnetsrvlib.py    From heralding with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, reader, writer, client_address, loop):
    """Constructor.

        When called without arguments, create an unconnected instance.
        With a hostname argument, it connects the instance; a port
        number is optional.
        """
    # Am I doing the echoing?
    self.loop = loop
    self.DOECHO = True
    # What opts have I sent DO/DONT for and what did I send?
    self.DOOPTS = {}
    # What opts have I sent WILL/WONT for and what did I send?
    self.WILLOPTS = {}
    self.reader = reader
    self.writer = writer
    # What commands does this CLI support
    self.rawq = b''  # Raw input string
    self.sbdataq = b''  # Sub-Neg string
    self.eof = 0  # Has EOF been reached?
    self.iacseq = b''  # Buffer for IAC sequence.
    self.sb = 0  # Flag for SB and SE sequence.
    self.history = []  # Command history
    self.cookedq = asyncio.Queue(loop=self.loop)
    super().__init__(reader, writer, client_address) 
Example #6
Source File: core.py    From p2p-python with MIT License 6 votes vote down vote up
def __init__(self, host=None, listen=15):
        assert V.DATA_PATH is not None, 'Setup p2p params before CoreClass init.'
        assert host is None or host == 'localhost'
        # status params
        self.f_stop = False
        self.f_finish = False
        self.f_running = False
        # working info
        self.start_time = int(time())
        self.number = 0
        self.user: List[User] = list()
        self.user_lock = asyncio.Lock()
        self.host = host  # local=>'localhost', 'global'=>None
        self.core_que = asyncio.Queue()
        self.backlog = listen
        self.traffic = Traffic()
        self.ping_status: Dict[int, asyncio.Event] = ExpiringDict(max_len=5000, max_age_seconds=900) 
Example #7
Source File: connection.py    From Telethon with MIT License 6 votes vote down vote up
def __init__(self, ip, port, dc_id, *, loop, loggers, proxy=None):
        self._ip = ip
        self._port = port
        self._dc_id = dc_id  # only for MTProxy, it's an abstraction leak
        self._loop = loop
        self._log = loggers[__name__]
        self._proxy = proxy
        self._reader = None
        self._writer = None
        self._connected = False
        self._send_task = None
        self._recv_task = None
        self._codec = None
        self._obfuscation = None  # TcpObfuscated and MTProxy
        self._send_queue = asyncio.Queue(1)
        self._recv_queue = asyncio.Queue(1) 
Example #8
Source File: domain_sn.py    From hsds with Apache License 2.0 6 votes vote down vote up
def __init__(self, app, domains, bucket=None, get_root=False, verbose=False, max_tasks_per_node=100):
        log.info(f"FolderCrawler.__init__  {len(domains)} domain names")
        self._app = app
        self._get_root = get_root
        self._verbose = verbose
        self._q = asyncio.Queue()
        self._domain_dict = {}
        self._group_dict = {}
        for domain in domains:
            self._q.put_nowait(domain)
        self._bucket = bucket
        max_tasks = max_tasks_per_node * app['node_count']
        if len(domains) > max_tasks:
            self._max_tasks = max_tasks
        else:
            self._max_tasks = len(domains) 
Example #9
Source File: _input.py    From pyuavcan with MIT License 6 votes vote down vote up
def __init__(self,
                 specifier:        pyuavcan.transport.InputSessionSpecifier,
                 payload_metadata: pyuavcan.transport.PayloadMetadata,
                 loop:             asyncio.AbstractEventLoop,
                 finalizer:        _base.SessionFinalizer):
        """Use the factory method."""
        self._specifier = specifier
        self._payload_metadata = payload_metadata

        self._queue: asyncio.Queue[CANInputSession._QueueItem] = asyncio.Queue()
        assert loop is not None
        self._loop = loop
        self._transfer_id_timeout_ns = int(CANInputSession.DEFAULT_TRANSFER_ID_TIMEOUT / _NANO)

        self._receivers = [_transfer_reassembler.TransferReassembler(nid, payload_metadata.max_size_bytes)
                           for nid in _node_id_range()]

        self._statistics = CANInputSessionStatistics()   # We could easily support per-source-node statistics if needed

        super(CANInputSession, self).__init__(finalizer=finalizer) 
Example #10
Source File: bot_test.py    From yui with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_bot_init(monkeypatch, fx_config):
    importlib = FakeImportLib()

    monkeypatch.setattr('importlib.import_module', importlib.import_module)

    fx_config.APPS = ['yui.app1', 'yui.app2']
    box = Box()
    bot = Bot(fx_config, using_box=box)

    assert bot.config == fx_config
    assert bot.channels == []
    assert bot.ims == []
    assert bot.groups == []
    assert bot.restart is False
    assert isinstance(bot.api, SlackAPI)
    assert bot.box is box
    assert isinstance(bot.queue, asyncio.Queue)
    assert importlib.import_queue == [
        'yui.app1',
        'yui.app2',
    ] 
Example #11
Source File: transaction_store.py    From indy-plenum with Apache License 2.0 6 votes vote down vote up
def reset(self):
        """
        Clear the values of all attributes of the transaction store.
        """
        self.getsCounter = 0

        # dictionary of processed requests for each client. Value for each
        # client is a dictionary with request id as key and transaction id as
        # value
        self.processedRequests = {}  # type: Dict[str, Dict[int, str]]

        # dictionary of responses to be sent for each client. Value for each
        # client is an asyncio Queue
        self.responses = {}  # type: Dict[str, asyncio.Queue]

        # dictionary with key as transaction id and `Reply` as
        # value
        self.transactions = {}  # type: Dict[str, Reply]

    # Used in test only. 
Example #12
Source File: _input.py    From pyuavcan with MIT License 6 votes vote down vote up
def __init__(self,
                 specifier:        pyuavcan.transport.InputSessionSpecifier,
                 payload_metadata: pyuavcan.transport.PayloadMetadata,
                 loop:             asyncio.AbstractEventLoop,
                 finalizer:        typing.Callable[[], None]):
        self._specifier = specifier
        self._payload_metadata = payload_metadata
        self._loop = loop
        self._maybe_finalizer: typing.Optional[typing.Callable[[], None]] = finalizer
        assert isinstance(self._specifier, pyuavcan.transport.InputSessionSpecifier)
        assert isinstance(self._payload_metadata, pyuavcan.transport.PayloadMetadata)
        assert isinstance(self._loop, asyncio.AbstractEventLoop)
        assert callable(self._maybe_finalizer)

        self._transfer_id_timeout = self.DEFAULT_TRANSFER_ID_TIMEOUT
        self._queue: asyncio.Queue[pyuavcan.transport.TransferFrom] = asyncio.Queue() 
Example #13
Source File: _subscriber.py    From pyuavcan with MIT License 6 votes vote down vote up
def __init__(self,
                 impl:           SubscriberImpl[MessageClass],
                 loop:           asyncio.AbstractEventLoop,
                 queue_capacity: typing.Optional[int]):
        """
        Do not call this directly! Use :meth:`Presentation.make_subscriber`.
        """
        if queue_capacity is None:
            queue_capacity = 0      # This case is defined by the Queue API. Means unlimited.
        else:
            queue_capacity = int(queue_capacity)
            if queue_capacity < 1:
                raise ValueError(f'Invalid queue capacity: {queue_capacity}')

        self._closed = False
        self._impl = impl
        self._loop = loop
        self._maybe_task: typing.Optional[asyncio.Task[None]] = None
        self._rx: _Listener[MessageClass] = _Listener(asyncio.Queue(maxsize=queue_capacity, loop=loop))
        impl.add_listener(self._rx)

    # ----------------------------------------  HANDLER-BASED API  ---------------------------------------- 
Example #14
Source File: server.py    From sanic with MIT License 6 votes vote down vote up
def on_body(self, body):
        if self.is_request_stream and self._is_stream_handler:
            # body chunks can be put into asyncio.Queue out of order if
            # multiple tasks put concurrently and the queue is full in python
            # 3.7. so we should not create more than one task putting into the
            # queue simultaneously.
            self._body_chunks.append(body)
            if (
                not self._request_stream_task
                or self._request_stream_task.done()
            ):
                self._request_stream_task = self.loop.create_task(
                    self.stream_append()
                )
        else:
            self.request.body_push(body) 
Example #15
Source File: _input.py    From pyuavcan with MIT License 6 votes vote down vote up
def __init__(self,
                 specifier:        pyuavcan.transport.InputSessionSpecifier,
                 payload_metadata: pyuavcan.transport.PayloadMetadata,
                 loop:             asyncio.AbstractEventLoop,
                 finalizer:        typing.Callable[[], None]):
        """
        Do not call this directly.
        Instead, use the factory method :meth:`pyuavcan.transport.serial.SerialTransport.get_input_session`.
        """
        self._specifier = specifier
        self._payload_metadata = payload_metadata
        self._loop = loop
        assert self._loop is not None

        if not isinstance(self._specifier, pyuavcan.transport.InputSessionSpecifier) or \
                not isinstance(self._payload_metadata, pyuavcan.transport.PayloadMetadata):  # pragma: no cover
            raise TypeError('Invalid parameters')

        self._statistics = SerialInputSessionStatistics()
        self._transfer_id_timeout = self.DEFAULT_TRANSFER_ID_TIMEOUT
        self._queue: asyncio.Queue[pyuavcan.transport.TransferFrom] = asyncio.Queue()
        self._reassemblers: typing.Dict[int, TransferReassembler] = {}

        super(SerialInputSession, self).__init__(finalizer) 
Example #16
Source File: test_asgi.py    From quart with MIT License 6 votes vote down vote up
def test_http_completion() -> None:
    # Ensure that the connecion callable returns on completion
    app = Quart(__name__)
    scope = {
        "headers": [(b"host", b"quart")],
        "http_version": "1.1",
        "method": "GET",
        "scheme": "https",
        "path": "/",
        "query_string": b"",
    }
    connection = ASGIHTTPConnection(app, scope)

    queue: asyncio.Queue = asyncio.Queue()
    queue.put_nowait({"type": "http.request", "body": b"", "more_body": False})

    async def receive() -> dict:
        # This will block after returning the first and only entry
        return await queue.get()

    async def send(message: dict) -> None:
        pass

    # This test fails if a timeout error is raised here
    await asyncio.wait_for(connection(receive, send), timeout=1) 
Example #17
Source File: iterators.py    From discord.py with MIT License 6 votes vote down vote up
def __init__(self, bot, limit, before=None, after=None):

        if isinstance(before, datetime.datetime):
            before = Object(id=time_snowflake(before, high=False))
        if isinstance(after, datetime.datetime):
            after = Object(id=time_snowflake(after, high=True))

        self.bot = bot
        self.limit = limit
        self.before = before
        self.after = after

        self._filter = None

        self.state = self.bot._connection
        self.get_guilds = self.bot.http.get_guilds
        self.guilds = asyncio.Queue()

        if self.before and self.after:
            self._retrieve_guilds = self._retrieve_guilds_before_strategy
            self._filter = lambda m: int(m['id']) > self.after.id
        elif self.after:
            self._retrieve_guilds = self._retrieve_guilds_after_strategy
        else:
            self._retrieve_guilds = self._retrieve_guilds_before_strategy 
Example #18
Source File: help_channels.py    From bot with MIT License 6 votes vote down vote up
def create_channel_queue(self) -> asyncio.Queue:
        """
        Return a queue of dormant channels to use for getting the next available channel.

        The channels are added to the queue in a random order.
        """
        log.trace("Creating the channel queue.")

        channels = list(self.get_category_channels(self.dormant_category))
        random.shuffle(channels)

        log.trace("Populating the channel queue with channels.")
        queue = asyncio.Queue()
        for channel in channels:
            queue.put_nowait(channel)

        return queue 
Example #19
Source File: network.py    From bale-bot-python with Apache License 2.0 6 votes vote down vote up
def __init__(self, token, base_url, incoming_queue=None, outgoing_queue=None, loop=None):

        self.logger = Logger.get_logger()
        self._base_url = base_url
        self._token = token
        self._running = False
        self._incoming_queue = incoming_queue or asyncio.Queue()
        self._outgoing_queue = outgoing_queue or asyncio.Queue()
        self._session = None
        self._ws = None
        self._loop = loop

        self._listener_task = None
        self._sender_task = None
        self._heartbeat = Config.heartbeat
        self._receive_timeout = Config.receive_timeout
        self.retry = 0 
Example #20
Source File: test_watchers.py    From aiozk with MIT License 6 votes vote down vote up
def test_watcher_fires_after_nonode(zk, data_watcher, child1):
    """
    Test that waiting for a nonexistent node is allowed if
    CREATED is in the watched events
    """
    messages = asyncio.Queue()
    data_watcher.watched_events.append(WatchEvent.CREATED)

    async def callback(d):
        print('Callback sees', d)
        await messages.put(d)

    # should trigger fetch right away, getting NoNode
    data_watcher.add_callback(child1, callback)

    no_node = await asyncio.wait_for(messages.get(), 1)
    assert no_node == NoNode

    # should trigger watch, which triggers fetch, which gets 'some data'
    await zk.create(child1, 'some data')
    some_data = await asyncio.wait_for(messages.get(), 1)
    assert some_data == b'some data'

    data_watcher.remove_callback(child1, callback)
    await zk.delete(child1) 
Example #21
Source File: _input.py    From pyuavcan with MIT License 5 votes vote down vote up
def frame_queue_capacity(self, value: typing.Optional[int]) -> None:
        if value is not None and not value > 0:
            raise ValueError(f'Invalid value for queue capacity: {value}')

        old_queue = self._queue
        self._queue = asyncio.Queue(int(value) if value is not None else 0, loop=self._loop)
        try:
            while True:
                self._push_frame(*old_queue.get_nowait())
        except asyncio.QueueEmpty:
            pass 
Example #22
Source File: aioclientsockssocket.py    From minikerberos with MIT License 5 votes vote down vote up
def sendrecv(self, data):
		self.out_queue = asyncio.Queue()
		self.in_queue = asyncio.Queue()
		comms = SocksQueueComms(self.out_queue, self.in_queue)

		self.client = SOCKSClient(comms, self.target.proxy.target, self.target.proxy.creds)
		self.proxy_task = asyncio.create_task(self.client.run())

		length = len(data).to_bytes(4, byteorder = 'big', signed = False)
		await self.out_queue.put(length+data)

		resp_data = b''
		resp_data_len = -1
		while True:
			data, err = await self.in_queue.get()
			if data is None:
				break
			if err is not None:
				raise err
			resp_data += data
			if resp_data_len == -1:
				if len(resp_data) > 4:
					resp_data_len = int.from_bytes(resp_data[:4], byteorder = 'big', signed = False)
					if resp_data_len == 0:
						raise Exception('Returned data length is 0! This means the server did not understand our message')
			
			if resp_data_len != -1:
				if len(resp_data) == resp_data_len + 4:
					resp_data = resp_data[4:]
					break
				elif len(resp_data) > resp_data_len + 4:
					raise Exception('Got too much data somehow')
				else:
					continue
		
		await self.out_queue.put(None)
		if resp_data == b'':
			raise Exception('Connection returned no data!')
		
		krb_message = KerberosResponse.load(resp_data)
		return krb_message 
Example #23
Source File: server_runner.py    From gabriel with Apache License 2.0 5 votes vote down vote up
def __init__(self, source_name, fresh_inputs_queue_size):
        self._source_name = source_name
        self._unsent_inputs = asyncio.Queue(maxsize=fresh_inputs_queue_size)
        self._latest_input = None
        self._engine_workers = set() 
Example #24
Source File: dispatcher.py    From bale-bot-python with Apache License 2.0 5 votes vote down vote up
def __init__(self, loop, token, base_url, bale_futures):

        self.logger = Logger.get_logger()
        self.incoming_queue = asyncio.Queue()
        self.outgoing_queue = asyncio.Queue()
        self.timeout = Config.request_timeout
        self.token = token
        self.bot = Bot(loop=loop,
                       token=token, base_url=base_url,
                       incoming_queue=self.incoming_queue,
                       outgoing_queue=self.outgoing_queue,
                       bale_futures=bale_futures,
                       timeout=self.timeout)

        self._bale_futures = bale_futures
        self.message_handlers = []
        self.error_handlers = []
        self.read_handler_object = None
        self.default_handler_object = None

        self.conversation_next_step_handlers = {}
        self.conversation_data = {}

        self.running = False

        self.is_batch_updates_processed = True
        self.last_seq = None
        self.real_time_fetch_updates = Config.real_time_fetch_updates
        self.continue_last_processed_seq = Config.continue_last_processed_seq
        self.timeInterval = Config.timeInterval
        self.last_poll_request_time = 0
        self.updates_number = Config.updates_number

        @self.message_handler(TextFilter(pattern=r"{}*".format(Config.monitoring_hash)))
        def handle_monitoring_msg(bot, update):
            monitoring_message = update.get_effective_message()
            monitoring_text = monitoring_message.text
            result_text = str(monitoring_text.split(Config.monitoring_hash)[1])
            result_message = TextMessage(text=result_text)
            bot.respond(update=update, message=result_message) 
Example #25
Source File: peer.py    From pyquarkchain with MIT License 5 votes vote down vote up
def msg_queue(self) -> "asyncio.Queue[PeerMessage]":
        if self._msg_queue is None:
            self._msg_queue = asyncio.Queue(maxsize=self.msg_queue_maxsize)
        return self._msg_queue 
Example #26
Source File: __init__.py    From aioh2 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, test, client_side, *, loop=None):
        super().__init__(client_side, loop=loop)
        test.server = self
        self.events = asyncio.Queue() 
Example #27
Source File: messaging.py    From sawtooth-core with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self._queue = asyncio.Queue()
        self._futures = {} 
Example #28
Source File: interconnect.py    From sawtooth-core with Apache License 2.0 5 votes vote down vote up
def start(self):
        complete_or_error_queue = queue.Queue()
        self._thread = InstrumentedThread(
            target=self._send_receive_thread.setup,
            args=(zmq.DEALER, complete_or_error_queue))
        self._thread.name = self.__class__.__name__ + self._thread.name

        self._thread.start()
        err = complete_or_error_queue.get(block=True)
        if err != _STARTUP_COMPLETE_SENTINEL:
            raise err 
Example #29
Source File: interconnect.py    From sawtooth-core with Apache License 2.0 5 votes vote down vote up
def start(self):
        complete_or_error_queue = queue.Queue()
        self._thread = InstrumentedThread(
            target=self._send_receive_thread.setup,
            args=(zmq.ROUTER, complete_or_error_queue))
        self._thread.name = self.__class__.__name__ + self._thread.name
        self._thread.start()
        # Blocking in startup until the background thread has made it to
        # running the event loop or error.
        err = complete_or_error_queue.get(block=True)
        if err != _STARTUP_COMPLETE_SENTINEL:
            raise err 
Example #30
Source File: base.py    From butian-src-domains with GNU General Public License v3.0 5 votes vote down vote up
def event_loop(self):
        q = asyncio.Queue()
        for request_item in self.request_list:
            q.put_nowait(request_item)
        loop = uvloop.new_event_loop()
        asyncio.set_event_loop(loop)
        tasks = [self.handle_tasks(task_id, q) for task_id in range(self.coroutine_num)]
        loop.run_until_complete(asyncio.wait(tasks))
        loop.close()