我有这样的代码:
from stomp import *
from stomp.listener import ConnectionListener
from stomp.utils import parse_frame
class MyListener(ConnectionListener):
_counter=0
def on_message(self, frame):
if self._counter > 10:
return
print(self._counter)
self._counter += 1
print('Starting...')
connection = Connection([('darwin-dist-44ae45.nationalrail.co.uk', '61613')])
connection.set_listener('', MyListener())
connection.connect(REDACTED)
connection.subscribe('/topic/darwin.pushport-v16', 11)
print('Ummm...???')
当我使用 Python 从命令行运行它时,带有
connection
的行不会执行:
$ python3 myscript.py
Starting...
Ummm...???
但是,当我打开 python shell 并一个一个地运行这些命令时,
connection.subscribe('/topic/darwin.pushport-v16', 11)
会按预期产生一堆输出:
$ python3
>>> from stomp import *
>>> from stomp.listener import ConnectionListener
>>> from stomp.utils import parse_frame
>>>
>>> class MyListener(ConnectionListener):
... _counter=0
... def on_message(self, frame):
... if self._counter > 10:
... return
... print(self._counter)
... self._counter += 1
...
>>> print('Starting...')
Starting...
>>> connection = Connection([('darwin-dist-44ae45.nationalrail.co.uk', '61613')])
>>> connection.set_listener('', MyListener())
>>> connection.connect('DARWINba0475e0-225d-49e3-8ba7-ee150d948992', 'c10e58e0-2d2d-4776-b4b8-f7e0e50047de')
>>> connection.subscribe('/topic/darwin.pushport-v16', 11)
0
1
2
我以前从未遇到过这样的奇怪行为。为什么会发生这种情况,我该如何解决? 谢谢