2017-06-22 233 views
0

书面菇的文件中,我运行这个例子,它的工作原理:如何在nameko中使用service runner?

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:[email protected]', 
} 

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 

回答

1

我怀疑你是在你的文件的顶部缺少import eventlet; eventlet.monkey_patch()。在文档中提到了Eventlet,但我认为它可能更清晰,并且可能会明确添加到某些示例中。

另外,您可以通过使模块只是你的服务定义和使用nameko run在命令行中使用类似的捆绑服务亚军(其为您处理此)在hello world example

相关问题