You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I am trying to create a Pika wrapper using AsyncioConnection, I want it to be async and reconnect in the background. If no connection is up, don't produce or consume events.
however, when providing my own asyncio.new_event_loop() to AsyncioConnection(custom_ioloop), I get an exception.
Code:
_on_connection_open_error is being called.
err is equal to = RabbitMQ Connection error AMQPConnectionError: (AMQPConnectionWorkflowFailed: 1 exceptions in all; last exception - AMQPConnectorSocketConnectError: NotImplementedError(); first exception - None,)
I extracted the following backtrace _unused_connection:
Traceback (most recent call last):
File "D:\Python39\lib\site-packages\pika\adapters\utils\io_services_utils.py", line 316, in _start_async
self._nbio.set_writer(self._sock.fileno(), self._on_writable)
File "D:\Python39\lib\site-packages\pika\adapters\asyncio_connection.py", line 194, in set_writer
self._loop.add_writer(fd, on_writable)
File "D:\Python39\lib\asyncio\events.py", line 510, in add_writer
raise NotImplementedError
NotImplementedError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python39\lib\site-packages\pika\adapters\utils\connection_workflow.py", line 815, in _try_next_resolved_address
addr_record = next(self._addrinfo_iter)
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "D:\Python39\lib\site-packages\pika\connection.py", line 1363, in basic_nack
return self.server_capabilities.get('basic.nack', False)
AttributeError: 'NoneType' object has no attribute 'get'
Code:
importjsonimporttracebackimportfunctoolsimportthreadingimportasyncioimportpikafrompika.adapters.asyncio_connectionimportAsyncioConnectionimportconfigclassRabbitMQConnection:
def__init__(self, connection_string, on_open_callback, on_error_callback):
self._on_open_callback=on_open_callbackself._on_error_callback=on_error_callbackself._ioloop=asyncio.new_event_loop()
self._connection_string=connection_stringself._connection=Noneself._connect()
self._reconnect_delay=0def_connect(self):
self._connection=AsyncioConnection(
parameters=pika.URLParameters(self._connection_string),
on_open_callback=self._on_connection_open,
on_open_error_callback=self._on_connection_open_error,
on_close_callback=self._on_connection_closed,
custom_ioloop=self._ioloop)
def_on_connection_open(self, connection):
self._on_open_callback(connection)
self._reconnect_delay=0def_on_connection_open_error(self, _unused_connection, err):
"""This method is called by pika if the connection to RabbitMQ can't be established. :param pika.SelectConnection _unused_connection: The connection :param Exception err: The error """print("RabbitMQ Connection error", repr(err))
self._on_error_callback(_unused_connection, err)
self._stopping=Falseself._stop()
def_on_connection_closed(self, _unused_connection, reason):
"""This method is invoked by pika when the connection to RabbitMQ is closed unexpectedly. Since it is unexpected, we will reconnect to RabbitMQ if it disconnects. :param pika.connection.Connection connection: The closed connection obj :param Exception reason: exception representing reason for loss of connection. """self._on_error_callback(_unused_connection, reason)
self._stop()
defstart(self):
defstart_loop(loop):
asyncio.set_event_loop(loop)
whileTrue:
try:
loop.run_forever()
self._connection.ioloop.run_forever()
except: traceback.print_exc()
thread=threading.Thread(target=start_loop, args=(self._ioloop,), daemon=True)
thread.start()
defstop(self):
self._stopping=Trueself._stop()
def_stop(self):
self._connection.ioloop.stop()
ifself._connection.is_open:
self._connection.close()
ifnotself._stopping:
self._ioloop.create_task(self._reconnect())
else:
# TODO: Close thread# asyncio.create_task()passasyncdef_reconnect(self):
connection_delay=self._get_reconnect_delay()
print("RabbitMQ Disconnected. Reconnecting in %d seconds."% (connection_delay))
awaitasyncio.sleep(connection_delay)
self._connect()
def_get_reconnect_delay(self):
self._reconnect_delay=min(self._reconnect_delay+1, 30)
returnself._reconnect_delayclassRabbitMQ:
def__init__(self,
rabbit_host=config.RABBIT_HOST,
rabbit_port=config.RABBIT_PORT,
rabbit_username=config.RABBIT_USERNAME,
rabbit_password=config.RABBIT_PASSWORD,
queue=config.RABBIT_QUEUE):
# Initializationself._queue=queueself._channel=Noneself._channel_open_callbacks= []
connection_string=f'amqp://{rabbit_username}:{rabbit_password}@{rabbit_host}:{rabbit_port}/%2f'print("Connecting to "+connection_string)
self._connection=RabbitMQConnection(connection_string, self._on_connection_open, self._on_connection_closed)
def_on_connection_open(self, connection):
connection.channel(on_open_callback=self._on_channel_open)
def_on_connection_closed(self, _unused_connection, reason_or_err):
self._channel=Nonedef_on_channel_open(self, channel):
self._channel=channelself._channel.queue_declare(queue=self.queue)
forcallbackinself._channel_open_callbacks:
callback(self._channel)
self._channel_open_callbacks= []
defstart(self):
self._connection.start()
defstop(self):
# TODO: Stop consumingself._connection.stop()
defconsume_keywords(self, callback):
kwargs=dict(queue=self._queue_name,
on_message_callback=callback,
auto_ack=True)
ifnotself._channel:
self._channel_open_callbacks.append(lambdachannel: channel.basic_consume(**kwargs))
returnself._channel.basic_consume(**kwargs)
defpublish_keywords(self, keywords):
keywords_list=list(iter(keywords))
iflen(keywords_list) ==0: returnifself._channel:
self._channel.basic_publish(exchange="",
routing_key=self._queue_name,
body=json.dumps(keywords_list),
properties=pika.BasicProperties(content_type='application/json', delivery_mode=1))
Thank you!
The text was updated successfully, but these errors were encountered:
Sorry, I was unsubscribed from this repository and have missed new issues from the past year. I will investigate this before the next major release, thank you!
Hi, I am trying to create a Pika wrapper using AsyncioConnection, I want it to be async and reconnect in the background. If no connection is up, don't produce or consume events.
however, when providing my own
asyncio.new_event_loop()
toAsyncioConnection(custom_ioloop)
, I get an exception.Code:
_on_connection_open_error is being called.
err is equal to =
RabbitMQ Connection error AMQPConnectionError: (AMQPConnectionWorkflowFailed: 1 exceptions in all; last exception - AMQPConnectorSocketConnectError: NotImplementedError(); first exception - None,)
I extracted the following backtrace _unused_connection:
Code:
Thank you!
The text was updated successfully, but these errors were encountered: