正如nameko的文档所述,我运行这个例子并且有效:
from nameko.runners import ServiceRunner
from nameko.testing.utils import get_container
from nameko.rpc import rpc
class ServiceA:
name = "service_a"
class ServiceB:
name = "service_b"
# create a runner for ServiceA and ServiceB
runner = ServiceRunner(config={})
runner.add_service(ServiceA)
runner.add_service(ServiceB)
# ``get_container`` will return the container for a particular service
container_a = get_container(runner, ServiceA)
# start both services
runner.start()
print('runner start')
# stop both services
runner.stop()
它显示:
runner start
但是当我在service_a中添加一个rpc方法,并在config中添加AMQP_URI时,它不起作用,看起来它没有连接到RabbitMQ。我该怎么办?
class ServiceA:
name = "service_a"
@rpc
def hello_a(self):
return 'hello service_a.'
config = {
'AMQP_URI': 'amqp://guest:guest@localhost',
}
runner = ServiceRunner(config=config)
当我按下Ctrl + C时:
Traceback (most recent call last):
File "/Users/apple/Documents/nameko_test/proxy.py", line 34, in <module>
runner.start()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/nameko/runners.py", line 65, in start
SpawningProxy(self.containers).start()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/nameko/utils/__init__.py", line 181, in spawning_method
return list(pool.imap(call, self._items))
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/greenpool.py", line 244, in next
val = self.waiters.get().wait()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/greenthread.py", line 179, in wait
return self._exit_event.wait()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/event.py", line 121, in wait
return hubs.get_hub().switch()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 295, in switch
return self.greenlet.switch()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 337, in run
self.fire_timers(self.clock())
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/hubs/hub.py", line 458, in fire_timers
timer()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/hubs/timer.py", line 58, in __call__
cb(*args, **kw)
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/greenthread.py", line 221, in main
self._resolve_links()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/greenthread.py", line 236, in _resolve_links
f(self, *ca, **ckw)
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/nameko/containers.py", line 458, in _handle_managed_thread_exited
self._handle_thread_exited(gt)
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/nameko/containers.py", line 462, in _handle_thread_exited
gt.wait()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/greenthread.py", line 179, in wait
return self._exit_event.wait()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/event.py", line 125, in wait
current.throw(*self._exc)
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/eventlet/greenthread.py", line 218, in main
result = function(*args, **kwargs)
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/kombu/mixins.py", line 177, in run
for _ in self.consume(limit=None): # pragma: no cover
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/kombu/mixins.py", line 199, in consume
conn.drain_events(timeout=safety_interval)
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/kombu/connection.py", line 288, in drain_events
return self.transport.drain_events(self.connection, **kwargs)
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/kombu/transport/pyamqp.py", line 95, in drain_events
return connection.drain_events(**kwargs)
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/amqp/connection.py", line 303, in drain_events
chanmap, None, timeout=timeout,
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/amqp/connection.py", line 366, in _wait_multiple
channel, method_sig, args, content = read_timeout(timeout)
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/amqp/connection.py", line 337, in read_timeout
return self.method_reader.read_method()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/amqp/method_framing.py", line 186, in read_method
self._next_method()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/amqp/method_framing.py", line 107, in _next_method
frame_type, channel, payload = read_frame()
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/amqp/transport.py", line 154, in read_frame
frame_header = read(7, True)
File "/Users/apple/Documents/my_projects/venv/lib/python3.6/site-packages/amqp/transport.py", line 277, in _read
s = recv(n - len(rbuf))
KeyboardInterrupt
答案 0 :(得分:1)
我怀疑您在文件顶部缺少import eventlet; eventlet.monkey_patch()
。文档中提到了Eventlet,但我认为它可能更清晰,并且可能明确地添加到一些示例中。
或者,您可以使用捆绑服务运行器(为您处理此问题),方法是创建一个仅包含服务定义的模块,并使用命令行中的nameko run
,如hello world example