2021-01-24 22:00:31 +08:00
|
|
|
|
2021-01-11 22:54:41 +08:00
|
|
|
import multiprocessing as mp
|
2021-01-24 22:00:31 +08:00
|
|
|
from multiprocessing.context import Process
|
2021-01-11 23:21:48 +08:00
|
|
|
import os
|
2021-01-24 19:00:19 +08:00
|
|
|
import time
|
2021-01-24 22:00:31 +08:00
|
|
|
import random
|
2021-01-11 23:21:48 +08:00
|
|
|
|
2021-01-11 22:54:41 +08:00
|
|
|
|
2021-01-11 23:13:50 +08:00
|
|
|
class Main(mp.Process):
|
2021-01-11 22:54:41 +08:00
|
|
|
def __init__(self, dev) -> None:
|
2021-01-11 23:13:50 +08:00
|
|
|
Process.__init__(self)
|
2021-01-11 22:54:41 +08:00
|
|
|
self.aaa = 1
|
2021-01-11 23:13:50 +08:00
|
|
|
self.ttt = True
|
2021-01-11 22:54:41 +08:00
|
|
|
self.dev = dev
|
|
|
|
|
2021-01-11 23:13:50 +08:00
|
|
|
def run(self):
|
2021-01-11 22:54:41 +08:00
|
|
|
while self.ttt:
|
|
|
|
print(os.getpid())
|
2021-01-15 21:22:34 +08:00
|
|
|
time.sleep(2)
|
2021-01-11 22:54:41 +08:00
|
|
|
while not(self.dev.using):
|
|
|
|
self.dev.using = True
|
2021-02-14 20:22:29 +08:00
|
|
|
print('开始调用a')
|
2021-01-11 23:13:50 +08:00
|
|
|
if self.dev.dev1 != 0:
|
|
|
|
self.aaa = self.dev.dev1
|
2021-01-15 21:22:34 +08:00
|
|
|
if self.dev.bget == False:
|
2021-02-14 20:22:29 +08:00
|
|
|
print('B 没拿到')
|
2021-01-11 22:54:41 +08:00
|
|
|
else:
|
2021-02-14 20:22:29 +08:00
|
|
|
print('B 拿到了')
|
2021-01-11 23:13:50 +08:00
|
|
|
self.dev.bget = False
|
2021-01-11 22:54:41 +08:00
|
|
|
self.dev.aget = True
|
2021-01-11 23:13:50 +08:00
|
|
|
self.ttt = self.dev.a
|
2021-01-11 22:54:41 +08:00
|
|
|
self.dev.using = False
|
2021-02-14 20:22:29 +08:00
|
|
|
print('结束调用a')
|
2021-01-11 23:13:50 +08:00
|
|
|
break
|
2021-02-14 20:22:29 +08:00
|
|
|
print(' aaa = ', self.aaa)
|
|
|
|
print('完事,收工---a')
|
2021-01-11 22:54:41 +08:00
|
|
|
|
|
|
|
|
2021-01-11 23:13:50 +08:00
|
|
|
class render(mp.Process):
|
2021-01-11 22:54:41 +08:00
|
|
|
def __init__(self, dev) -> None:
|
2021-01-11 23:13:50 +08:00
|
|
|
Process.__init__(self)
|
2021-01-11 23:21:48 +08:00
|
|
|
self.bbb = 3
|
2021-01-11 22:54:41 +08:00
|
|
|
self.ggg = True
|
2021-01-11 23:13:50 +08:00
|
|
|
self.dev = dev
|
2021-01-11 23:21:48 +08:00
|
|
|
|
2021-01-11 23:13:50 +08:00
|
|
|
def run(self):
|
|
|
|
while self.ggg:
|
|
|
|
print(os.getpid())
|
2021-01-15 21:22:34 +08:00
|
|
|
time.sleep(2)
|
2021-01-11 23:13:50 +08:00
|
|
|
while not(self.dev.using):
|
|
|
|
self.dev.using = True
|
|
|
|
if random.choice(range(0, 5, 1)) == 4:
|
|
|
|
self.dev.dev1 = self.bbb
|
2021-02-14 20:22:29 +08:00
|
|
|
print('开始调用b')
|
2021-01-15 21:22:34 +08:00
|
|
|
if self.dev.aget == False:
|
2021-02-14 20:22:29 +08:00
|
|
|
print('A 没拿到')
|
2021-01-11 23:13:50 +08:00
|
|
|
else:
|
2021-02-14 20:22:29 +08:00
|
|
|
print('A 拿到了')
|
2021-01-11 23:13:50 +08:00
|
|
|
self.dev.aget = False
|
2021-01-15 21:22:34 +08:00
|
|
|
# self.dev.bget = True
|
2021-01-11 23:13:50 +08:00
|
|
|
self.ggg = self.dev.b
|
|
|
|
self.dev.using = False
|
2021-02-14 20:22:29 +08:00
|
|
|
print('结束调用b')
|
2021-01-11 23:13:50 +08:00
|
|
|
break
|
2021-02-14 20:22:29 +08:00
|
|
|
print('完事,收工---b')
|
2021-01-11 22:54:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Delivery():
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.using = False
|
2021-01-11 23:13:50 +08:00
|
|
|
self.dev1 = 0
|
|
|
|
self.a = True
|
|
|
|
self.b = True
|
2021-01-11 22:54:41 +08:00
|
|
|
self.aget = False
|
|
|
|
self.bget = False
|
|
|
|
|
|
|
|
|
2021-02-14 20:22:29 +08:00
|
|
|
if __name__ == '__main__':
|
2021-01-11 22:54:41 +08:00
|
|
|
dev = Delivery()
|
2021-01-11 23:13:50 +08:00
|
|
|
A = Main(dev)
|
|
|
|
B = render(dev)
|
2021-02-14 20:22:29 +08:00
|
|
|
print(':aaa')
|
2021-01-11 23:13:50 +08:00
|
|
|
A.start()
|
2021-01-15 21:22:34 +08:00
|
|
|
time.sleep(1)
|
2021-01-11 23:13:50 +08:00
|
|
|
B.start()
|
2021-01-15 21:22:34 +08:00
|
|
|
time.sleep(5)
|
2021-02-14 20:22:29 +08:00
|
|
|
print('gogogogogogoogogogogo')
|
2021-01-11 23:21:48 +08:00
|
|
|
dev.a = False
|
2021-02-14 20:22:29 +08:00
|
|
|
print('A is deadddddddddddddddddddd AAAAAAAA')
|
2021-01-11 23:21:48 +08:00
|
|
|
time.sleep(3)
|
|
|
|
dev.b = False
|
2021-02-14 20:22:29 +08:00
|
|
|
print('BBBBBBBBBBBBBB')
|