A
A
Andrey2021-06-12 15:57:37
Python
Andrey, 2021-06-12 15:57:37

Why is variable change not visible from Python process?

Hello!

I posted a simple example code:

import os
import time
from multiprocessing import Pool, Process

my_list = []


def do(x):
    my_list.append(x)
    time.sleep(.5)
    return x


def write_info():
    while True:
        os.system('clear')
        print('My list count:', len(my_list))
        time.sleep(1)


if __name__ == '__main__':
    p_info = Process(target=write_info)
    p_info.start()

    with Pool(5) as p:
        result = p.map(do, range(100))

    p_info.join()


Displays constantly: My list count: 0

Tell me what's the problem?
Why is the change of the my_list variable not visible from the process?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-06-12
@andrey_u

Because it is not necessary to use global variables.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question