1

正如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 方法,并在配置中添加一个 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
4

1 回答 1

3

import eventlet; eventlet.monkey_patch()我怀疑您在文件顶部丢失了。文档中提到了 Eventlet,但我认为它可能更清晰,并且可能明确添加到一些示例中。

或者,您可以使用捆绑的服务运行程序(它为您处理此问题),方法是使用您的服务定义制作一个模块并nameko run从命令行使用,如hello world 示例

于 2017-06-24T10:17:14.520 回答