Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added consumer sync callbacks support. #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions aioamqp/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from . import constants as amqp_constants
from . import frame as amqp_frame
from . import exceptions
from .compat import iscoroutinepartial
from .envelope import Envelope


Expand Down Expand Up @@ -579,7 +580,7 @@ def basic_consume(self, callback, queue_name='', consumer_tag='', no_local=False
the callback will be called each time we're receiving a message.

Args:
callback: coroutine, the called callback
callback: callable, the called callback
queue_name: str, the queue to receive message from
consumer_tag: str, optional consumer tag
no_local: bool, if set the server will not send messages
Expand Down Expand Up @@ -656,7 +657,11 @@ def basic_deliver(self, frame):
yield from event.wait()
del self._ctag_events[consumer_tag]

yield from callback(self, body, envelope, properties)
if iscoroutinepartial(callback):
yield from callback(self, body, envelope, properties)
else:
self._loop.call_soon(callback, self, body, envelope, properties)


@asyncio.coroutine
def server_basic_cancel(self, frame):
Expand Down
14 changes: 14 additions & 0 deletions aioamqp/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@
from asyncio import ensure_future
except ImportError:
ensure_future = asyncio.async


def iscoroutinepartial(fn):
# http://bugs.python.org/issue23519

while True:
parent = fn

fn = getattr(parent, 'func', None)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while this code seems just fine, it would be better to update the state in one line like this:

parent, fn = fn, getattr(parent, 'func', None)

see https://youtu.be/OSGv2VnC0go?t=34m for lengthier explanation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to do first iteration?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initializing outside of the loop seems like a good idea and would also avoid using True:

parent = fn
while fn is not None:
    parent, fn = fn, getattr(parent, 'func', None)


if fn is None:
break

return asyncio.iscoroutinefunction(parent)
30 changes: 30 additions & 0 deletions aioamqp/tests/test_consume.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def setUp(self):
def callback(self, channel, body, envelope, properties):
self.consume_future.set_result((body, envelope, properties))

def callback_sync(self, channel, body, envelope, properties):
self.consume_future.set_result((body, envelope, properties))

@asyncio.coroutine
def get_callback_result(self):
yield from self.consume_future
Expand Down Expand Up @@ -86,6 +89,33 @@ def test_consume(self):
self.assertEqual(b"coucou", body)
self.assertIsInstance(properties, Properties)

@testing.coroutine
def test_consume_not_coroutine(self):
# declare
yield from self.channel.queue_declare("q", exclusive=True, no_wait=False)
yield from self.channel.exchange_declare("e", "fanout")
yield from self.channel.queue_bind("q", "e", routing_key='')

# get a different channel
channel = yield from self.create_channel()

# publish
yield from channel.publish("coucou", "e", routing_key='',)

yield from asyncio.sleep(2, loop=self.loop)
# start consume
yield from channel.basic_consume(self.callback_sync, queue_name="q")
# required ?
yield from asyncio.sleep(2, loop=self.loop)

self.assertTrue(self.consume_future.done())
# get one
body, envelope, properties = yield from self.get_callback_result()
self.assertIsNotNone(envelope.consumer_tag)
self.assertIsNotNone(envelope.delivery_tag)
self.assertEqual(b"coucou", body)
self.assertIsInstance(properties, Properties)

@testing.coroutine
def test_big_consume(self):
# declare
Expand Down