How to have just one instance of a dependency?

I'm converting a flask app to a nameko service, in the old flask all there
was a globally instantiated class which takes about 5 seconds to init.

I've refactored this into a nameko dependency, roughly looking like:

class SlowThing:
  def __init__(self):
      time.sleep(5)

class MyThing(DependencyProvider):

    def get_dependency(self, worker_ctx):
        return SlowThing()

class MyService:
    name = 'my_service'
    rivescript = MyThing()

With the above, SlowThing() will be init'd for each request to the service.

What's the best way to have just one instance of SlowThing() upon service
startup? Should I make that class a singleton? Or is there a better
recommended way?

Thanks in advance.

Hi Richard,

Following the example in e.g.
https://github.com/onefinestay/nameko-sqlalchemy/blob/master/nameko_sqlalchemy/database_session.py
you can instantiate your SlowThing in `MyThing.setup()`, and attach it to
self, and then give workers access to that from `get_dependency` (note that
this requires `SlowThing` instances to be threadsafe, i.e. safe for sharing
by multiple concurrent workers)

Best,
David

···

On Friday, 10 February 2017 11:32:23 UTC, Richard wrote:

I'm converting a flask app to a nameko service, in the old flask all there
was a globally instantiated class which takes about 5 seconds to init.

I've refactored this into a nameko dependency, roughly looking like:

class SlowThing:
  def __init__(self):
      time.sleep(5)

class MyThing(DependencyProvider):

    def get_dependency(self, worker_ctx):
        return SlowThing()

class MyService:
    name = 'my_service'
    rivescript = MyThing()

With the above, SlowThing() will be init'd for each request to the
service.

What's the best way to have just one instance of SlowThing() upon service
startup? Should I make that class a singleton? Or is there a better
recommended way?

Thanks in advance.

Perfect thanks. And yes it's thread safe

···

On Friday, February 10, 2017 at 11:36:06 AM UTC, David Szotten wrote:

Hi Richard,

Following the example in e.g.
https://github.com/onefinestay/nameko-sqlalchemy/blob/master/nameko_sqlalchemy/database_session.py
you can instantiate your SlowThing in `MyThing.setup()`, and attach it to
self, and then give workers access to that from `get_dependency` (note that
this requires `SlowThing` instances to be threadsafe, i.e. safe for sharing
by multiple concurrent workers)

Best,
David

On Friday, 10 February 2017 11:32:23 UTC, Richard wrote:

I'm converting a flask app to a nameko service, in the old flask all
there was a globally instantiated class which takes about 5 seconds to init.

I've refactored this into a nameko dependency, roughly looking like:

class SlowThing:
  def __init__(self):
      time.sleep(5)

class MyThing(DependencyProvider):

    def get_dependency(self, worker_ctx):
        return SlowThing()

class MyService:
    name = 'my_service'
    rivescript = MyThing()

With the above, SlowThing() will be init'd for each request to the
service.

What's the best way to have just one instance of SlowThing() upon
service startup? Should I make that class a singleton? Or is there a better
recommended way?

Thanks in advance.