[Python] Python3 dictionary being modified at another trhead does not show changes after those...

Discussão em 'Python' iniciado por Stack, Julho 14, 2025.

  1. Stack

    Stack Membro Participativo

    Python version: (3.9, but the same result with python 3.12)

    The goal was that another thread modified a dictionary and those modifications to be available at the original thread.

    import multiprocessing as mp
    import sys

    def my_func(result: dict):
    print(f'variable address at worker: {hex(id(result))}', file=sys.stderr)
    result["test"] = "test"
    print(f'{result}', file=sys.stderr)

    result = {}
    print(f'variable address at main thread: {hex(id(result))}', file=sys.stderr)
    my_worker = lambda : my_func(result)

    # execution at another Thread
    p = mp.Process(target=my_worker)
    p.start()
    p.join()

    print(f'result at main thread after execution: {result}', file=sys.stderr)

    # manual execution
    my_worker()
    print(f'result at main thread after manual execution: {result}', file=sys.stderr)

    print(sys.version)


    and the output is:

    variable address at main thread: 0x6ffffff39580
    variable address at worker: 0x6ffffff39580
    {'test': 'test'}
    result at main thread after execution: {}
    result at main thread after manual execution: {'test': 'test'}
    3.9.16 (main, Mar 8 2023, 22:47:22)
    [GCC 11.3.0]


    My expectations were that result dictionary would show the changes done at the worker but it does not.

    What am I doing wrong?

    Continue reading...

Compartilhe esta Página