Skip to content

Using injector with multi-thread application #185

@ddanecki

Description

@ddanecki

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions