injector icon indicating copy to clipboard operation
injector copied to clipboard

Using injector with multi-thread application

Open ddanecki opened this issue 3 years ago • 1 comments

It's not an issue rather a question. I hope you won't mind me posting it.

I'm trying to figure out what would be the best way to use the injector in an application where I need to spawn multiple threads. Currently, the app has one thread only and in the main.py, I put the entire application together using injector modules.

Therefore, I call only:

my_app = injector.get(MyApplication)
my_app.run()

and the application does the job.

In the run function, I'd like to create a couple of threads doing some work. I have a multi-provider with all the workers I want to run.

The problem is, that the entire application is already put together at the moment I call the get method of the injector. Therefore all the objects already exist and will not be created again (since they are injected into __init__ functions of all objects, starting with MyApplication). That's leading to an issue where I try to use objects created in a different thread and some of them don't really like it.

How should I approach this? Should I call the get method in each spawned thread or there is some other way to do it?

There's the ThreadLocalScope which seems to be something to look into, however as the MyApplication object is created before the threads are spawned, from that point onward, only one instance of each worker exists.

Injecting objects into the run function would seem like a way to overcome the problem, but that's gone as far as I understand and wasn't the proposed way of using the injector in the first place.

Any help would be appreciated.

ddanecki avatar Jun 22 '21 12:06 ddanecki

Hey! Yeah, it's perfectly fine just to ask questions here.

My initial thought would be that you could inject ProviderOf[X] where X is what you need and only call the provider in the code executing in another thread or something, but honestly I'm not sure I fully grasp your situation. Can you provide some minimal code that demonstrates the problem? (may be artificial, I don't mind, as long as it's code)

jstasiak avatar Jun 30 '21 01:06 jstasiak