Git Product home page Git Product logo

Comments (1)

mosquito avatar mosquito commented on August 15, 2024

Hello. Try this.

import asyncio
import logging
from aio_pika import connect

logging.basicConfig(level=logging.DEBUG)

conn = None


def on_connection_closed(future):
    try:
        future.result()
    except:
        logging.exception("Oooops...")
    loop.stop()


async def rmq_connect():
    global conn
    loop = asyncio.get_event_loop()
    conn = await connect('amqp://guest:[email protected]/', loop=loop)
    conn.add_close_callback(on_connection_closed)


async def close():
    global conn
    await conn.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(rmq_connect())

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    loop.run_until_complete(close())

loop.close()

Run this script and write in another terminal:

rabbitmqctl stop_app && rabbitmqctl start_app

And the function on_connection_closed will be called immediately.

Traceback (most recent call last):
  File "example.py", line 12, in on_connection_closed
    future.result()
  File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
ConnectionError: [Errno CONNECTION_FORCED - broker forced connection closure with reason 'shutdown'] 320```

from aio-pika.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.