Python3:成功处理邮件后,NSQ断开连接

时间:2019-01-25 13:05:58

标签: python python-3.x message-queue nsq

我有一个使用官方Python NSQ客户端处理数据的NSQ代码,需要一些时间来处理。

为了防止断开连接,我在整个循环的代码中使用message.touch(),以防止断开连接。

有时候,队列中的当前项目已成功清除,但是下一个项目似乎已断开连接,即使它一直进行到没有任何错误,也会自动重新排队。

我已附上了错误日志,该错误日志仅在当前项目完成后发生,而下一项只是以失败的message.touch()开始。

我将lookupd_poll_interval= 15与同步队列一起使用。

框架代码为:

import nsq

def handler( message ):
    data = json.loads( message.body.decode( "utf-8" ) )[ "data" ]
    for item in data:
        message.touch()
        process_this_item( item )

    message.finish()
    return True

r = nsq.Reader( message_handler= handler,
                lookupd_http_addresses= [ 'http://127.0.0.1:4161' ],
                topic= 'test_channel_1',
                channel= 'test_channel_1',
                lookupd_poll_interval= 15 )
nsq.run()

请让我知道如何避免此问题。

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 290, in _read_body
    self.trigger(event.DATA, conn=self, data=data)
File "/usr/local/lib/python3.5/dist-packages/nsq/event.py", line 84, in trigger
    ev(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 503, in _on_data
    self.send(protocol.nop())
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 296, in send
    self.stream.write(self.encoder.encode(data))
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 387, in write
    self._check_closed()
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 925, in _check_closed
    raise StreamClosedError(real_error=self.error)
tornado.iostream.StreamClosedError: Stream is closed
2019-01-25 12:54:45:ERROR:nsq.conn uncaught exception in data event
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 290, in _read_body
    self.trigger(event.DATA, conn=self, data=data)
File "/usr/local/lib/python3.5/dist-packages/nsq/event.py", line 84, in trigger
    ev(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 503, in _on_data
    self.send(protocol.nop())
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 296, in send
    self.stream.write(self.encoder.encode(data))
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 387, in write
    self._check_closed()
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 925, in _check_closed
    raise StreamClosedError(real_error=self.error)
tornado.iostream.StreamClosedError: Stream is closed
2019-01-25 12:54:45:ERROR:nsq.conn uncaught exception in data event
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 290, in _read_body
    self.trigger(event.DATA, conn=self, data=data)
File "/usr/local/lib/python3.5/dist-packages/nsq/event.py", line 84, in trigger
    ev(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 503, in _on_data
    self.send(protocol.nop())
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 296, in send
    self.stream.write(self.encoder.encode(data))
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 387, in write
    self._check_closed()
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 925, in _check_closed
    raise StreamClosedError(real_error=self.error)
tornado.iostream.StreamClosedError: Stream is closed
2019-01-25 12:54:45:ERROR:nsq.conn uncaught exception in data event
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 290, in _read_body
    self.trigger(event.DATA, conn=self, data=data)
File "/usr/local/lib/python3.5/dist-packages/nsq/event.py", line 84, in trigger
    ev(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 503, in _on_data
    self.send(protocol.nop())
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 296, in send
    self.stream.write(self.encoder.encode(data))
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 387, in write
    self._check_closed()
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 925, in _check_closed
    raise StreamClosedError(real_error=self.error)
tornado.iostream.StreamClosedError: Stream is closed
2019-01-25 12:54:45:ERROR:nsq.client [nsq-worker-1:4150:test_channel_1:test_channel_1] ERROR: ConnectionClosedError('Stream is closed',)
2019-01-25 12:54:45:WARNING:nsq.reader [nsq-worker-1:4150:test_channel_1:test_channel_1] connection closed
2019-01-25 12:54:45:ERROR:nsq.conn uncaught exception in data event
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 290, in _read_body
    self.trigger(event.DATA, conn=self, data=data)
File "/usr/local/lib/python3.5/dist-packages/nsq/event.py", line 84, in trigger
    ev(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 503, in _on_data
    self.send(protocol.nop())
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 296, in send
    self.stream.write(self.encoder.encode(data))
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 387, in write
    self._check_closed()
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 925, in _check_closed
    raise StreamClosedError(real_error=self.error)
tornado.iostream.StreamClosedError: Stream is closed
2019-01-25 12:54:45:ERROR:nsq.conn uncaught exception in data event
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 290, in _read_body
    self.trigger(event.DATA, conn=self, data=data)
File "/usr/local/lib/python3.5/dist-packages/nsq/event.py", line 84, in trigger
    ev(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 503, in _on_data
    self.send(protocol.nop())
File "/usr/local/lib/python3.5/dist-packages/nsq/conn.py", line 296, in send
    self.stream.write(self.encoder.encode(data))
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 387, in write
    self._check_closed()
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 925, in _check_closed
    raise StreamClosedError(real_error=self.error)
tornado.iostream.StreamClosedError: Stream is closed
2019-01-25 12:54:45:ERROR:nsq.client [nsq-worker-0:4150:test_channel_1:test_channel_1] ERROR: SendError: failed to send RDY 1 (Stream is closed)
2019-01-25 12:54:45:INFO:root Succesfully connected to database [ system_config ].
2019-01-25 12:54:46:INFO:root Successfully retrieved config details for config_id [ 2 ].
2019-01-25 12:54:46:INFO:root Hash for config_id [ 2 ] successfully obtained: [ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ]
2019-01-25 12:54:46:ERROR:nsq.client [nsq-worker-0:4150:test_channel_1:test_channel_1] ERROR: SendError: failed to send TOUCH b'0ad5b8177dcee000' (Stream is closed)

0 个答案:

没有答案