Interprocess variables are not shared, but if and when there is a need shared variable, you can borrow multiprocessing method module, here to introduce the implementation method of the module:
(a) shared variable
[AppleScript]
plain text view Copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
|
import multiprocessing
def func
(
num
)
:
num.
value
=
20
if
__name__
=
=
"__main__"
:
num
=
multiprocessing.Value
(
"d"
,
10.0
)
print
(
num.
value
)
p
=
multiprocessing.Process
(
target
=
func
,
args
=
(
num
,
)
)
p.start
(
)
p.join
(
)
print
(
num.
value
)
|
(B) shared array
[AppleScript]
plain text view Copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
import multiprocessing
def func
(
arr
)
:
arr[
0
]
=
6
arr[
1
]
=
7
if
__name__
=
=
"__main__"
:
arr
=
multiprocessing.Array
(
"i"
,
[
1
,
2
,
3
,
4
,
5
]
)
print
(
arr[
:
]
)
p
=
multiprocessing.Process
(
target
=
func
,
args
=
(
arr
,
)
)
p.start
(
)
p.join
(
)
print
(
arr[
:
]
)
|
(Iii) the share list
[AppleScript]
plain text view Copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
import multiprocessing
def func
(
mylist
)
:
mylist.append
(
11
)
mylist.append
(
22
)
mylist.append
(
33
)
if
__name__
=
=
"__main__"
:
mylist
=
multiprocessing.Manager
(
)
.
list
(
range
(
5
)
)
p
=
multiprocessing.Process
(
target
=
func
,
args
=
(
mylist
,
)
)
p.start
(
)
p.join
(
)
print
(
mylist
)
|
(D) Share Dictionary
[AppleScript]
plain text view Copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
import multiprocessing
def func
(
mydict
)
:
mydict['
name
']
=
'python'
mydict['age']
=
20
if
__name__
=
=
"__main__"
:
mydict
=
multiprocessing.Manager
(
)
.dict
(
)
p
=
multiprocessing.Process
(
target
=
func
,
args
=
(
mydict
,
)
)
p.start
(
)
p.join
(
)
print
(
mydict
)
|
More technical information may concern: gzitcast