Discuss / Python / 进程有自己的地址空间,能读取但是不能写进程外的全局变量

进程有自己的地址空间,能读取但是不能写进程外的全局变量

Topic source

```

# -*- coding: utf-8 -*-

#!usr/bin/python

from multiprocessing import Process

import os

run_check = ['not run']

def run_proc(name):

    global run_check

    print(run_check)

    print('Run child process %s (%s)...' % (name, os.getpid()))

    run_check[0] = 'run already'

    print(run_check)

    print(id(run_check))

if __name__ == '__main__':

    print('Parent process %s.' % os.getpid())

    p = Process(target = run_proc, args = ('test',)) #注意这个IDE里面运行是不生效的

    print('Child process will start.')

    p.start()

    p.join()

    print('Child process end.')

    print(run_check)

    print(id(run_check))

    run_proc('test')

    print(run_check)

    print(id(run_check))

```

###运行结果

```

Parent process 8440.

Child process will start.

['not run']

Run child process test (6188)...

['run already']

2483552520712

Child process end.

['not run']

2247150240648

['not run']

Run child process test (8440)...

['run already']

2247150240648

['run already']

2247150240648

```

进程有自己的地址空间,能读取但是不能写进程外的全局变量


  • 1

Reply