Skip to content

Unable to initialize instance that are picklable in the container #734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
arunbalasubramani opened this issue Aug 3, 2023 · 2 comments

Comments

@arunbalasubramani
Copy link

arunbalasubramani commented Aug 3, 2023

Getting the following error while initializing the container that contains instances that cannot be pickled. Is there a requirement that all the instances created within the container should be picklable?

Version used: 4.41.0

class DependencyContainer(containers.DeclarativeContainer):
    metric_flow_client = providers.Singleton(
        MetricFlowClient,
        sql_client=SnowflakeSqlClient.from_config(mf_snowflake_config)
    )

if __name__ == "__main__":
    container = DependencyContainer().   <---- Fails here
    container.init_resources()
    container.wire(modules=[__name__])

Error log:

Traceback (most recent call last):
  File "/Users/arunkumar/Projects/unified-metrics-platform/consumption_grpc/grpc_server.py", line 79, in <module>
    container = DependencyContainer()
  File "src/dependency_injector/containers.pyx", line 730, in dependency_injector.containers.DeclarativeContainer.__new__
  File "src/dependency_injector/providers.pyx", line 4913, in dependency_injector.providers.deepcopy
  File "src/dependency_injector/providers.pyx", line 4920, in dependency_injector.providers.deepcopy
  File "/Users/arunkumar/.pyenv/versions/3.9.10/lib/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/Users/arunkumar/.pyenv/versions/3.9.10/lib/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/Users/arunkumar/.pyenv/versions/3.9.10/lib/python3.9/copy.py", line 153, in deepcopy
    y = copier(memo)
  File "src/dependency_injector/providers.pyx", line 2835, in dependency_injector.providers.BaseSingleton.__deepcopy__
  File "src/dependency_injector/providers.pyx", line 4920, in dependency_injector.providers.deepcopy
  File "/Users/arunkumar/.pyenv/versions/3.9.10/lib/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/Users/arunkumar/.pyenv/versions/3.9.10/lib/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/Users/arunkumar/.pyenv/versions/3.9.10/lib/python3.9/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/Users/arunkumar/.pyenv/versions/3.9.10/lib/python3.9/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/Users/arunkumar/.pyenv/versions/3.9.10/lib/python3.9/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/Users/arunkumar/.pyenv/versions/3.9.10/lib/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/Users/arunkumar/.pyenv/versions/3.9.10/lib/python3.9/copy.py", line 161, in deepcopy
    rv = reductor(4)
TypeError: cannot pickle '_thread.lock' object

Thanks for the help!

@Nefendi
Copy link

Nefendi commented Sep 25, 2023

I've had the same problem, but with pymongo's Collection class. Ended up using providers.Callable and wrapping the object creation in a function, as they are picklable:

from dependency_injector import containers, providers
from pymongo.collection import Collection

MONGO_DATABASE = ...
COLLECTION_NAME = ...


class FooRepository:
    pass


def _create_collection():
    return Collection(
        database=MONGO_DATABASE,
        name=COLLECTION_NAME,
    )


class FooContainer(containers.DeclarativeContainer):
    foo_repository = providers.Factory(
        FooRepository,
        collection=providers.Callable(_create_collection),
    )

It's not an ideal solution, but I hope it helps!

@pot-code
Copy link

pot-code commented Apr 6, 2025

i got the same issue with redis:

redis_client = providers.Factory(Redis, host=config().redis_host, port=6379, db=0)
worker_id_assigner = providers.Singleton(WorkerIdAssigner, redis_client=redis_client())
dependency_injector.errors.NonCopyableArgumentError: Couldn't copy keyword argument redis_client for provider <dependency_injector.providers.Singleton(<class 'app.common.uuid.snowflake.WorkerIdAssigner'>) at 0x105acf460>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants