python-injector / python-injector/injector

Using injector with multi-thread application

Open
#185 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
94
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with main.py, MyApplication.run, the multi-provider setup, and the ThreadLocalScope documentation or implementation. Trace when objects are created relative to the spawned threads, then define the recommended injector usage for this case and document what lifecycle and scope behavior users should expect.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.