# running nameko service in a single script

**URL:** <https://discourse.nameko.io/t/running-nameko-service-in-a-single-script/192>\
**Category:** googlegroup\
**Created:** [May 15, 2017, 3:19am UTC](https://discourse.nameko.io/t/running-nameko-service-in-a-single-script/192 "2017-05-15T03:19:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Selanac1991](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@Selanac1991](https://discourse.nameko.io/u/Selanac1991)\
**Post date:** [May 15, 2017, 3:19am UTC](https://discourse.nameko.io/t/running-nameko-service-in-a-single-script/192/1 "2017-05-15T03:19:48Z")

</div>

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](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:  
&nbsp;&nbsp;&nbsp;&nbsp;name = "service"

&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
&nbsp;&nbsp;&nbsp;&nbsp;def ping(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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"):  
&nbsp;&nbsp;&nbsp;&nbsp;with ServiceRpcProxy('service', config=amqp\_config) as cluster\_rpc:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p = cluster\_rpc.ping()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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!

---

<div class="post-metadata">

**Author:** ![Selanac1991](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@Selanac1991](https://discourse.nameko.io/u/Selanac1991)\
**Post date:** [May 15, 2017, 3:50am UTC](https://discourse.nameko.io/t/running-nameko-service-in-a-single-script/192/2 "2017-05-15T03:50:06Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;from nameko.rpc import rpc  
&nbsp;&nbsp;&nbsp;&nbsp;from nameko.standalone.rpc import ServiceRpcProxy  
&nbsp;&nbsp;&nbsp;&nbsp;from nameko.testing.services import entrypoint\_waiter  
&nbsp;&nbsp;&nbsp;&nbsp;from nameko.containers import ServiceContainer

&nbsp;&nbsp;&nbsp;&nbsp;class Service:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = "service"

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

&nbsp;&nbsp;&nbsp;&nbsp;amqp\_config = {'AMQP\_URI': 'amqp://guest:guest@localhost'}  
&nbsp;&nbsp;&nbsp;&nbsp;container = ServiceContainer(Service, config=amqp\_config)  
&nbsp;&nbsp;&nbsp;&nbsp;container.start()  
&nbsp;&nbsp;&nbsp;&nbsp;print "Container started!" # Doesn't get to this point

&nbsp;&nbsp;&nbsp;&nbsp;with entrypoint\_waiter(container, "ping"):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with ServiceRpcProxy('service', config=amqp\_config) as cluster\_rpc:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p = cluster\_rpc.ping()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert p == "Ping!"

&nbsp;&nbsp;&nbsp;&nbsp;container.stop()

if \_\_name\_\_ == "\_\_main\_\_":  
&nbsp;&nbsp;&nbsp;&nbsp;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](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:  
> > &nbsp;&nbsp;&nbsp;&nbsp;name = "service"
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;def ping(self):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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"):  
> > &nbsp;&nbsp;&nbsp;&nbsp;with ServiceRpcProxy('service', config=amqp\_config) as cluster\_rpc:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p = cluster\_rpc.ping()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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!

---

<div class="post-metadata">

**Author:** ![David\_Szotten](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/david_szotten/32/6_2.png) [@David\_Szotten](https://discourse.nameko.io/u/David_Szotten)\
**Post date:** [May 15, 2017, 8:22am UTC](https://discourse.nameko.io/t/running-nameko-service-in-a-single-script/192/3 "2017-05-15T08:22:55Z")

</div>

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():
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;from nameko.rpc import rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;from nameko.standalone.rpc import ServiceRpcProxy  
> > &nbsp;&nbsp;&nbsp;&nbsp;from nameko.testing.services import entrypoint\_waiter  
> > &nbsp;&nbsp;&nbsp;&nbsp;from nameko.containers import ServiceContainer
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;class Service:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = "service"
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ping(self):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "Ping!"
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;amqp\_config = {'AMQP\_URI': 'amqp://guest:guest@localhost'}  
> > &nbsp;&nbsp;&nbsp;&nbsp;container = ServiceContainer(Service, config=amqp\_config)  
> > &nbsp;&nbsp;&nbsp;&nbsp;container.start()  
> > &nbsp;&nbsp;&nbsp;&nbsp;print "Container started!" # Doesn't get to this point
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;with entrypoint\_waiter(container, "ping"):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with ServiceRpcProxy('service', config=amqp\_config) as cluster\_rpc:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p = cluster\_rpc.ping()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert p == "Ping!"
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;container.stop()
> > 
> > if \_\_name\_\_ == "\_\_main\_\_":  
> > &nbsp;&nbsp;&nbsp;&nbsp;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](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:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;name = "service"
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > > &nbsp;&nbsp;&nbsp;&nbsp;def ping(self):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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"):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;with ServiceRpcProxy('service', config=amqp\_config) as cluster\_rpc:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p = cluster\_rpc.ping()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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!

---

<div class="post-metadata">

**Author:** ![Diego](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Diego](https://discourse.nameko.io/u/Diego)\
**Post date:** [May 15, 2017, 6:34pm UTC](https://discourse.nameko.io/t/running-nameko-service-in-a-single-script/192/4 "2017-05-15T18:34:36Z")

</div>

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():
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;from nameko.rpc import rpc  
> > > &nbsp;&nbsp;&nbsp;&nbsp;from nameko.standalone.rpc import ServiceRpcProxy  
> > > &nbsp;&nbsp;&nbsp;&nbsp;from nameko.testing.services import entrypoint\_waiter  
> > > &nbsp;&nbsp;&nbsp;&nbsp;from nameko.containers import ServiceContainer
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;class Service:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = "service"
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ping(self):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return "Ping!"
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;amqp\_config = {'AMQP\_URI': 'amqp://guest:guest@localhost'}  
> > > &nbsp;&nbsp;&nbsp;&nbsp;container = ServiceContainer(Service, config=amqp\_config)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;container.start()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;print "Container started!" # Doesn't get to this point
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;with entrypoint\_waiter(container, "ping"):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with ServiceRpcProxy('service', config=amqp\_config) as  
> > > cluster\_rpc:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p = cluster\_rpc.ping()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert p == "Ping!"
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;container.stop()
> > > 
> > > if \_\_name\_\_ == "\_\_main\_\_":  
> > > &nbsp;&nbsp;&nbsp;&nbsp;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](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:  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;name = "service"
> > > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;def ping(self):  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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"):  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;with ServiceRpcProxy('service', config=amqp\_config) as cluster\_rpc:  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p = cluster\_rpc.ping()  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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!
