running nameko service in a single script

Hi, I'm trying to do something fairly basic in a single python file:
declare a service, start it, and then hit one of it's rpc endpoints.
However, the script hangs after I "start" the service (see below). I'm
following the examples posted at the bottom of this page
<http://nameko.readthedocs.io/en/stable/key_concepts.html>

from nameko.rpc import rpc
from nameko.standalone.rpc import ServiceRpcProxy
from nameko.testing.services import entrypoint_waiter
from nameko.containers import ServiceContainer

class Service:
    name = "service"

    @rpc
    def ping(self):
        return "Ping!"

amqp_config = {'AMQP_URI': 'amqp://guest:guest@localhost'}
container = ServiceContainer(Service, config=amqp_config)

container.start()
print "Container started!" ############################### Doesn't get to
this point ######################

with entrypoint_waiter(container, "ping"):
    with ServiceRpcProxy('service', config=amqp_config) as cluster_rpc:
        p = cluster_rpc.ping()
        print p

container.stop()

The script doesn't get past the "start()" call. I figure I'm doing
something dumb, but I'm not sure what.

Also, if I remove the "ping" endpoint from the service, container.start()
works fine. So it seems like I shouldn't be calling container.start() if I
have an rpc endpoint as a part of my service?

Thanks for your help!

I just noticed that if I run this as a pytest (see script below), it works.
But if I run it as a regular python script, it doesn't work. Any idea why
that would be? (Running pytest test.py works, but running python test.py
hangs.)

def test_runner():

    from nameko.rpc import rpc
    from nameko.standalone.rpc import ServiceRpcProxy
    from nameko.testing.services import entrypoint_waiter
    from nameko.containers import ServiceContainer

    class Service:
        name = "service"

        @rpc
        def ping(self):
            return "Ping!"

    amqp_config = {'AMQP_URI': 'amqp://guest:guest@localhost'}
    container = ServiceContainer(Service, config=amqp_config)
    container.start()
    print "Container started!" # Doesn't get to this point

    with entrypoint_waiter(container, "ping"):
        with ServiceRpcProxy('service', config=amqp_config) as cluster_rpc:
            p = cluster_rpc.ping()
            assert p == "Ping!"

    container.stop()

if __name__ == "__main__":
    test_runner()

···

On Sunday, May 14, 2017 at 8:19:48 PM UTC-7, selan...@gmail.com wrote:

Hi, I'm trying to do something fairly basic in a single python file:
declare a service, start it, and then hit one of it's rpc endpoints.
However, the script hangs after I "start" the service (see below). I'm
following the examples posted at the bottom of this page
<http://nameko.readthedocs.io/en/stable/key_concepts.html&gt;

from nameko.rpc import rpc
from nameko.standalone.rpc import ServiceRpcProxy
from nameko.testing.services import entrypoint_waiter
from nameko.containers import ServiceContainer

class Service:
    name = "service"

    @rpc
    def ping(self):
        return "Ping!"

amqp_config = {'AMQP_URI': 'amqp://guest:guest@localhost'}
container = ServiceContainer(Service, config=amqp_config)

container.start()
print "Container started!" ############################### Doesn't get to
this point ######################

with entrypoint_waiter(container, "ping"):
    with ServiceRpcProxy('service', config=amqp_config) as cluster_rpc:
        p = cluster_rpc.ping()
        print p

container.stop()

The script doesn't get past the "start()" call. I figure I'm doing
something dumb, but I'm not sure what.

Also, if I remove the "ping" endpoint from the service, container.start()
works fine. So it seems like I shouldn't be calling container.start() if I
have an rpc endpoint as a part of my service?

Thanks for your help!

Hi,

You are missing the eventlet monkey patching. This is one of the reasons we
provide the `nameko run` helper. This is also done for you by the nameko
pytest plugin

For your script to run, you need to insert `import eventlet;
eventlet.monkey_patch()` at the top of your file

Best,
David

···

On Monday, 15 May 2017 04:50:07 UTC+1, selan...@gmail.com wrote:

I just noticed that if I run this as a pytest (see script below), it
works. But if I run it as a regular python script, it doesn't work. Any
idea why that would be? (Running pytest test.py works, but running python
test.py hangs.)

def test_runner():

    from nameko.rpc import rpc
    from nameko.standalone.rpc import ServiceRpcProxy
    from nameko.testing.services import entrypoint_waiter
    from nameko.containers import ServiceContainer

    class Service:
        name = "service"

        @rpc
        def ping(self):
            return "Ping!"

    amqp_config = {'AMQP_URI': 'amqp://guest:guest@localhost'}
    container = ServiceContainer(Service, config=amqp_config)
    container.start()
    print "Container started!" # Doesn't get to this point

    with entrypoint_waiter(container, "ping"):
        with ServiceRpcProxy('service', config=amqp_config) as cluster_rpc:
            p = cluster_rpc.ping()
            assert p == "Ping!"

    container.stop()

if __name__ == "__main__":
    test_runner()

On Sunday, May 14, 2017 at 8:19:48 PM UTC-7, selan...@gmail.com wrote:

Hi, I'm trying to do something fairly basic in a single python file:
declare a service, start it, and then hit one of it's rpc endpoints.
However, the script hangs after I "start" the service (see below). I'm
following the examples posted at the bottom of this page
<http://nameko.readthedocs.io/en/stable/key_concepts.html&gt;

from nameko.rpc import rpc
from nameko.standalone.rpc import ServiceRpcProxy
from nameko.testing.services import entrypoint_waiter
from nameko.containers import ServiceContainer

class Service:
    name = "service"

    @rpc
    def ping(self):
        return "Ping!"

amqp_config = {'AMQP_URI': 'amqp://guest:guest@localhost'}
container = ServiceContainer(Service, config=amqp_config)

container.start()
print "Container started!" ############################### Doesn't get to
this point ######################

with entrypoint_waiter(container, "ping"):
    with ServiceRpcProxy('service', config=amqp_config) as cluster_rpc:
        p = cluster_rpc.ping()
        print p

container.stop()

The script doesn't get past the "start()" call. I figure I'm doing
something dumb, but I'm not sure what.

Also, if I remove the "ping" endpoint from the service, container.start()
works fine. So it seems like I shouldn't be calling container.start() if I
have an rpc endpoint as a part of my service?

Thanks for your help!

Awesome, that worked!

···

On Monday, May 15, 2017 at 1:22:56 AM UTC-7, David Szotten wrote:

Hi,

You are missing the eventlet monkey patching. This is one of the reasons
we provide the `nameko run` helper. This is also done for you by the nameko
pytest plugin

For your script to run, you need to insert `import eventlet;
eventlet.monkey_patch()` at the top of your file

Best,
David

On Monday, 15 May 2017 04:50:07 UTC+1, selan...@gmail.com wrote:

I just noticed that if I run this as a pytest (see script below), it
works. But if I run it as a regular python script, it doesn't work. Any
idea why that would be? (Running pytest test.py works, but running python
test.py hangs.)

def test_runner():

    from nameko.rpc import rpc
    from nameko.standalone.rpc import ServiceRpcProxy
    from nameko.testing.services import entrypoint_waiter
    from nameko.containers import ServiceContainer

    class Service:
        name = "service"

        @rpc
        def ping(self):
            return "Ping!"

    amqp_config = {'AMQP_URI': 'amqp://guest:guest@localhost'}
    container = ServiceContainer(Service, config=amqp_config)
    container.start()
    print "Container started!" # Doesn't get to this point

    with entrypoint_waiter(container, "ping"):
        with ServiceRpcProxy('service', config=amqp_config) as
cluster_rpc:
            p = cluster_rpc.ping()
            assert p == "Ping!"

    container.stop()

if __name__ == "__main__":
    test_runner()

On Sunday, May 14, 2017 at 8:19:48 PM UTC-7, selan...@gmail.com wrote:

Hi, I'm trying to do something fairly basic in a single python file:
declare a service, start it, and then hit one of it's rpc endpoints.
However, the script hangs after I "start" the service (see below). I'm
following the examples posted at the bottom of this page
<http://nameko.readthedocs.io/en/stable/key_concepts.html&gt;

from nameko.rpc import rpc
from nameko.standalone.rpc import ServiceRpcProxy
from nameko.testing.services import entrypoint_waiter
from nameko.containers import ServiceContainer

class Service:
    name = "service"

    @rpc
    def ping(self):
        return "Ping!"

amqp_config = {'AMQP_URI': 'amqp://guest:guest@localhost'}
container = ServiceContainer(Service, config=amqp_config)

container.start()
print "Container started!" ############################### Doesn't get
to this point ######################

with entrypoint_waiter(container, "ping"):
    with ServiceRpcProxy('service', config=amqp_config) as cluster_rpc:
        p = cluster_rpc.ping()
        print p

container.stop()

The script doesn't get past the "start()" call. I figure I'm doing
something dumb, but I'm not sure what.

Also, if I remove the "ping" endpoint from the service,
container.start() works fine. So it seems like I shouldn't be calling
container.start() if I have an rpc endpoint as a part of my service?

Thanks for your help!