程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python os. The fork module copies all memory data of the parent process and assigns a new address

編輯:Python

os.fork Except inheriting everything from the parent process , And the addresses of these contents are reallocated in memory , Be similar to python Medium deep copy
Let's talk about it first copy,deepcopy Content

import os
import copy
from copy import deepcopy
a=100
b='12123'
c=[1,2,3,4]
print(id(a),id(b),id(c))
acp=a
bcp=b
ccp=c
print(id(acp),id(bcp),id(ccp))

Running results

(python38) [[email protected] crawler]# python testfork.py 
9736032 139855151961008 139855151961280
9736032 139855151961008 139855151961280

You can see it Python The allocated memory addresses are the same
If the code is rewritten like this

import os
import copy
from copy import deepcopy
a=100
b='12123'
c=[1,2,3,4]
d=100
print(id(a),id(b),id(c),id(d))
acp=a
bcp=b
ccp=c
print(id(acp),id(bcp),id(ccp))
acp=deepcopy(a)
bcp=deepcopy(b)
ccp=c
print(id(acp),id(bcp),id(ccp))
acp=200
bcp='12123'
ccp[1]=2000
print(id(acp),id(bcp),id(ccp))
acp=400
bcp='155sdfd'
ccp[1]=50200
print(id(acp),id(bcp),id(ccp),ccp,c)
acp=8520
bcp='155ddasdfd'
ccp=deepcopy(c)
ccp[1]=50000
print(id(acp),id(bcp),id(ccp),ccp,ccp)

Running results

(python38) [[email protected] crawler]# python testfork.py 
9736032 140681963512816 140681963513088 9736032
9736032 140681963512816 140681963513088
9736032 140681963512816 140681963513088
9739232 140681963512816 140681963513088
140681963906960 140681836478384 140681963513088 [1, 50200, 3, 4] [1, 50200, 3, 4]
140681963907152 140681836478320 140681963377920 [1, 50000, 3, 4] [1, 50000, 3, 4]

You can see the characters , plastic float Class data is assigned if the values are the same , So it's the same address , Once the value changes , The memory address will also change , Similar to the relationship between ID card and holder , No matter whether the person is cosmetic surgery or disfigurement, the ID number of the person is that number . The common copy and deep copy memory addresses are the same
The list is of reference type , Plain light copy , The address of another variable is just the address of the original data ,deepcopy Will reallocate memory addresses , The data points to the new content address , At this time, the operation list and the original list are two operations , Mutual interference
With the above foundation, let's introduce os.fork

import os,time
a=100
b='12123'
c=[1,2,3,4]
print(' Lord -1',id(a),id(b),id(c))
p=os.fork()
if p==0:
a=200
b='2323'
c[1]=100
time.sleep(10)
print(' Son ',id(a), id(b), id(c),a,b,c)
print(' Lord -2',id(a),id(b),id(c),a,b,c)

Running results

import os,time
a=100
b='12123'
c=[1,2,3,4]
print(' Lord -1',id(a),id(b),id(c))
p=os.fork()
if p==0:
a=200
b='2323'
c[1]=100
time.sleep(10)
print(' Son ',id(a), id(b), id(c),a,b,c)
print(' Lord -2',id(a),id(b),id(c),a,b,c)

The memory addresses of all variables have been modified , Whether it's string,int,float Or a list
os.fork The main process and child processes continue as follows


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved