benjamin-hodgson / asynqp

An AMQP library for asyncio
MIT License
84 stars 29 forks source link

Function methods should also raise an error if used after closing #62

Open tvoinarovskyi opened 9 years ago

tvoinarovskyi commented 9 years ago

The usecase I want to cover is like so:

import asyncio
import asynqp

@asyncio.coroutine
def main_coro(loop):
    # connect to the RabbitMQ broker
    connection = yield from asynqp.connect(
        'localhost', 5672, username='guest', password='guest')

    # Open a communications channel
    channel = yield from connection.open_channel()
    yield from channel.declare_queue('test.queue')
    exchange = yield from channel.declare_exchange("", "direct")

    msg = asynqp.Message({'hello': 'world'})

    yield from channel.close()
    # Expected to raise error!!!
    exchange.publish(msg, 'test.queue')

    yield from connection.close()

def main():
    loop = asyncio.get_event_loop()
    main_task = asyncio.async(main_coro(loop))
    try:
        loop.run_until_complete(main_task)
    except asyncio.CancelledError:
        pass

if __name__ == "__main__":
    main()

Methods, that come to mind:

Any ideas how to gracefully do it?