Giter Club home page Giter Club logo

Comments (6)

laike9m avatar laike9m commented on June 24, 2024

Hi matt, it seems neither invoking start() on container or runner will start service unless I call wait(). Is this expected?

from nameko.

mattbennett avatar mattbennett commented on June 24, 2024

@laike9m start() does start the service, but you must wait() on the container or the runner to stop your process or thread from exiting.

The best way to run a service is nameko run on the command line. Or, if you want to embed the service in another application, wrap the run function in nameko.cli.run -- https://github.com/onefinestay/nameko/blob/master/nameko/cli/run.py#L117 (which is what the command line tool uses under the hood)

from nameko.

laike9m avatar laike9m commented on June 24, 2024

Thank you. I'm using a runner because in this way I'm able to stop the service programmatically. Actually there's another issue: I have to call kill() to stop it while stop() doesn't work.

from nameko.

mattbennett avatar mattbennett commented on June 24, 2024

Sorry, I forgot about this thread.

stop() waits for all running workers and managed threads to exit gracefully. You can't expose container.stop() in an entrypoint and expect to the container to stop when you call it -- this would create a deadlock between the container trying to stop and the worker or managed thread running the stop() method.

If you want to expose an entrypoint that is capable of stopping the container, you'd need to do it "out of band" (i.e. using a thread that wasn't created by the container)

Here is a toy example that does that:

from nameko.rpc import rpc
from nameko.extensions import DependencyProvider
import eventlet

class Stopper(DependencyProvider):

    def stop(self):
        # can't use container.spawn_managed_thread here either
        eventlet.spawn_n(self.container.stop)

    def get_dependency(self, worker_ctx):
        return self


class Service(object):
    name = "stop"

    stopper = Stopper()

    @rpc
    def stop(self):
        self.stopper.stop()
        return "stopping"
(tmp-6d6e28416a4d3fa9)Matts-13-inch-Macbook-Pro:tmp mattbennett$ nameko run stop
starting services: stop
Connected to amqp://guest:**@127.0.0.1:5672//
(tmp-6d6e28416a4d3fa9)Matts-13-inch-Macbook-Pro:~ mattbennett$ nameko shell
Nameko Python 2.7.10 (default, Jul  6 2015, 15:19:48)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] shell on darwin
Broker: amqp://guest:guest@localhost
>>> n.rpc.stop.stop()

from nameko.

davidszotten avatar davidszotten commented on June 24, 2024

it seems unusual to expose an endpoint to remotely shut down a service. is this definitely what you want when you say "stop the service programatically". Typically this is done e.g. via a process manager like supervisor, which stops (or kills) the service via posix signals. E.g. nameko run listens for SIGTERM and calls stop

from nameko.

mattbennett avatar mattbennett commented on June 24, 2024

Yes indeed, the example above is not a demonstration of how you should achieve this. It is only meant to illustrate an "out of band" thread.

Generating a SIGTERM signal is a much better way to have the container to stop gracefully.

from nameko.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.