# How to have just one instance of a dependency?

**URL:** https://discourse.nameko.io/t/how-to-have-just-one-instance-of-a-dependency/170
**Category:** googlegroup
**Created:** [February 10, 2017, 11:32am UTC](https://discourse.nameko.io/t/how-to-have-just-one-instance-of-a-dependency/170 "2017-02-10T11:32:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Richard](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@Richard](https://discourse.nameko.io/u/Richard)
#### Post date: [February 10, 2017, 11:32am UTC](https://discourse.nameko.io/t/how-to-have-just-one-instance-of-a-dependency/170/1 "2017-02-10T11:32:22Z")

</div>

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:  
&nbsp;&nbsp;def \_\_init\_\_(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(5)

class MyThing(DependencyProvider):

&nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SlowThing()

class MyService:  
&nbsp;&nbsp;&nbsp;&nbsp;name = 'my\_service'  
&nbsp;&nbsp;&nbsp;&nbsp;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.

---

<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: [February 10, 2017, 11:36am UTC](https://discourse.nameko.io/t/how-to-have-just-one-instance-of-a-dependency/170/2 "2017-02-10T11:36:05Z")

</div>

Hi Richard,

Following the example in e.g.  
[https://github.com/onefinestay/nameko-sqlalchemy/blob/master/nameko\_sqlalchemy/database\_session.py](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:  
> > &nbsp;&nbsp;def \_\_init\_\_(self):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(5)
> > 
> > class MyThing(DependencyProvider):
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SlowThing()
> > 
> > class MyService:  
> > &nbsp;&nbsp;&nbsp;&nbsp;name = 'my\_service'  
> > &nbsp;&nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

### Author: ![Richard](https://avatars.discourse-cdn.com/v4/letter/r/f0a364/32.png) [@Richard](https://discourse.nameko.io/u/Richard)
#### Post date: [February 10, 2017, 11:37am UTC](https://discourse.nameko.io/t/how-to-have-just-one-instance-of-a-dependency/170/3 "2017-02-10T11:37:37Z")

</div>

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](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:  
> > > &nbsp;&nbsp;def \_\_init\_\_(self):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(5)
> > > 
> > > class MyThing(DependencyProvider):
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return SlowThing()
> > > 
> > > class MyService:  
> > > &nbsp;&nbsp;&nbsp;&nbsp;name = 'my\_service'  
> > > &nbsp;&nbsp;&nbsp;&nbsp;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.
