An asynchronous queue for requesting data
Project description
Requeue
In applications that receive messages, like chat bots, where you run callbacks on incoming messages, you might want to capture follow-up messages within a callback. A request queue allows you to do that.
Examples
from requeue import Requeue
queue = Requeue()
Where the messages come in and are processed;
async def run_callbacks(message):
# If the message completes a request, since it is forwarded,
# it should not continue to be handled by the callbacks.
if await queue.complete(message):
continue
# If no request was completed, the message can be processed as usual.
for callback in client.callbacks:
await callback(message)
Inside a callback, to request and await an incoming message, you use wait_for
.
To only accept a message that meets specific criteria, you can pass a function
as a filter to the check
parameter.
@client.callback
async def on_message(message):
# An example of a knock knock joke back-and-forth,
# using the request queue to pick out the responses
if message == 'Knock knock!':
await client.send("Who's there?")
who = await queue.wait_for()
await client.send(f'{who} who?')
punchline = await queue.wait_for()
await punchline.react('😂')
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
requeue-0.1.0.tar.gz
(14.3 kB
view hashes)
Built Distribution
requeue-0.1.0-py3-none-any.whl
(14.9 kB
view hashes)