服务器之家

服务器之家 > 正文

python多进程共享变量

时间:2020-08-18 10:09     来源/作者:geosson

本文实例为大家分享了python多进程共享变量的相关代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from multiprocessing import Process, Manager
import os
import time
 
 
class MulFun():
 
  def __init__(self):
    self.a = [1,2,3,4,5]
    self.b = 0
    self.c = {}
    self.s = "hello world"
    self.radius = Manager().dict()
    self.radius['a'] = self.a
    self.radius['b'] = self.b
    self.radius['c'] = self.c
    self.radius['s'] = self.s
 
 
  def func1(self):
    self.b = self.radius['b']
    for i in range(10):
      self.b += i
      self.radius['b'] = self.b
      time.sleep(0.5)
      print '1: ', self.radius['b'],':', self.radius['c'], os.getpid()
 
 
  def func2(self):
    self.c = self.radius['c']
    for i in ['ab', 'bc', 'cd', 'df', 'fg']:
      self.c[i] = i + i
      self.radius['c'] = self.c
      time.sleep(0.5)
      print '2:  ', self.radius['b'],':', self.radius['c'], os.getpid()
 
  def run(self):
    process1 = Process(target=self.func1, args=())
    process2 = Process(target=self.func2, args=())
    process1.daemon = True
    process2.daemon = True
    process1.start()
    process2.start()
    process1.join()
    process2.join()
 
 
class MulSun1:
 
  def __init__(self, radius):
    self.radius = radius
    self.a = radius['a']
    self.s = radius['s']
 
  def process(self):
 
    for i, j in enumerate(self.a):
      #self.a[i] = j * 2
      #self.radius['a'] = self.a
      time.sleep(0.5)
      print '1: ', self.radius['a'], ' & ', self.radius['s']
 
 
class MulSun2:
 
  def __init__(self, radius):
    self.radius = radius
    self.a = radius['a']
    self.s = radius['s']
 
  def process(self):
 
    for i in range(10):
      self.s = self.s + ':% s' %i
      if i < len(self.a):
        self.a[i] += i
      else:
        self.a.append(i + i)
      self.radius['s'] = self.s
      self.radius['a'] = self.a
      time.sleep(0.5)
      print '2: ', self.radius['a'], ' & ', self.radius['s']
 
if __name__ == '__main__':
  aa = MulFun()
  s1 = MulSun1(aa.radius)
  s2 = MulSun2(aa.radius)
  process1 = Process(target=s1.process, args=())
  process2 = Process(target=s2.process, args=())
  process1.daemon = True
  process2.daemon = True
  process1.start()
  process2.start()
  process1.join()
  process2.join()
  print "------------------------"
  print 'process id:', os.getpid()
  print 'done'
  print aa.radius['a'], ' & ', aa.radius['s']

以上就是本文的全部内容,希望对大家学习python程序设计有所帮助。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
返回顶部