本文整理匯總了Python中asyncio.Queue方法的典型用法代碼示例。如果您正苦於以下問題:Python asyncio.Queue方法的具體用法?Python asyncio.Queue怎麽用?Python asyncio.Queue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類asyncio
的用法示例。
在下文中一共展示了asyncio.Queue方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sse
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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
示例2: test_http_completion
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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)
示例3: test_task_local
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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
示例4: on_body
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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)
示例5: __init__
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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)
示例6: __init__
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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)
示例7: __init__
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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())
示例8: create_channel_queue
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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
示例9: __init__
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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
示例10: test_watcher_fires_after_nonode
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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)
示例11: __init__
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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
示例12: __init__
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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 ----------------------------------------
示例13: __init__
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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()
示例14: __init__
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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)
示例15: __init__
# 需要導入模塊: import asyncio [as 別名]
# 或者: from asyncio import Queue [as 別名]
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)