pika 使用的坑

最近使用pika的异步模式,一直报错,错误提示如下:

Traceback (most recent call last):
  File "mq_test1.py", line 36, in <module>
    connection.ioloop.start()
  File "/usr/local/lib/python3.6/site-packages/pika/adapters/select_connection.py", line 543, in start
    self._poller.start()
  File "/usr/local/lib/python3.6/site-packages/pika/adapters/select_connection.py", line 804, in start
    self._process_timeouts()
  File "/usr/local/lib/python3.6/site-packages/pika/adapters/select_connection.py", line 494, in process_timeouts
    self._timer.process_timeouts()
  File "/usr/local/lib/python3.6/site-packages/pika/adapters/select_connection.py", line 329, in process_timeouts
    timeout.callback()
  File "/usr/local/lib/python3.6/site-packages/pika/adapters/utils/connection_workflow.py", line 341, in _on_overall_timeout
    self._amqp_conn.close(320, msg)
  File "/usr/local/lib/python3.6/site-packages/pika/connection.py", line 1283, in close
    raise exceptions.ConnectionWrongStateError(msg)
pika.exceptions.ConnectionWrongStateError: Illegal close(320, "Timeout while setting up AMQP to '192.168.122.1'/(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('192.168$
122.1', 5672)); ssl=False") request on <SelectConnection CLOSED transport=None params=<URLParameters host=192.168.122.1 port=5672 virtual_host=test ssl=False>> because it was called whil$
 connection state=CLOSED.

源码如下:

import pika


def on_open(connection):
   connection.channel(on_channel_open)


import time as timer
from time import time


def on_channel_open(channel):
   message = 'message body value' * 100
   start = time()
   for i in range(5):
       channel.basic_publish('test_exchange',
                           '',
                           message,
                           pika.BasicProperties(content_type='text/plain',
                                                delivery_mode=2))
       if  i % 1000 == 0:
           print('publish', i)
#         timer.sleep(10)
   end = time() - start
   print(end)

# Step #1: Connect to RabbitMQ
parameters = pika.URLParameters('amqp://test:test@192.168.122.1:5672/test')

connection = pika.SelectConnection(parameters=parameters,
                                  on_open_callback=on_open)

try:

   # Step #2 - Block on the IOLoop
   connection.ioloop.start()

# Catch a Keyboard Interrupt to make sure that the connection is closed cleanly
except KeyboardInterrupt:

   # Gracefully close the connection
   connection.close()

   # Start the IOLoop again so Pika can communicate, it will stop on its own when the connection is closed
   connection.ioloop.start()

这是因为pika的channel函数原型为如下形式
在这里插入图片描述
on_open_callback 不再是位置参数,而是关键字参数,所以在调用是不能使用connection.channel(on_channel_open) 而是要改为connection.channel(on_open_callback=on_channel_open)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值