1

As written in nameko's document, I run this example and it works:

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()

It shows:

runner start

but when i add a rpc method in service_a, and add a AMQP_URI in config, it doesn't work, looks like it doesn't connected to RabbitMQ. What should i do?

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)

when i press 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
Blackbelly
  • 11
  • 2

1 Answers1

3

I suspect you are missing import eventlet; eventlet.monkey_patch() at the top of your file. Eventlet is mentioned in the documentation but I think it could be clearer, and possibly explicitly added to some of the examples.

Alternatively you could use the bundled service runner (which handles this for you) by making a module with just your service definitions and using nameko run from the command line like in the hello world example

second
  • 28,029
  • 7
  • 75
  • 76