2021-01-11 22:54:41 +08:00
|
|
|
import multiprocessing as mp
|
2021-01-11 23:21:48 +08:00
|
|
|
import os
|
|
|
|
import random
|
2021-01-24 19:00:19 +08:00
|
|
|
import time
|
|
|
|
from multiprocessing.context import Process
|
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-01-11 23:13:50 +08:00
|
|
|
print("开始调用a")
|
|
|
|
if self.dev.dev1 != 0:
|
|
|
|
self.aaa = self.dev.dev1
|
2021-01-15 21:22:34 +08:00
|
|
|
if self.dev.bget == False:
|
2021-01-11 22:54:41 +08:00
|
|
|
print("B 没拿到")
|
|
|
|
else:
|
|
|
|
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-01-11 23:13:50 +08:00
|
|
|
print("结束调用a")
|
|
|
|
break
|
|
|
|
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
|
|
|
|
print("开始调用b")
|
2021-01-15 21:22:34 +08:00
|
|
|
if self.dev.aget == False:
|
2021-01-11 23:13:50 +08:00
|
|
|
print("A 没拿到")
|
|
|
|
else:
|
|
|
|
print("A 拿到了")
|
|
|
|
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
|
|
|
|
print("结束调用b")
|
|
|
|
break
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
dev = Delivery()
|
2021-01-11 23:13:50 +08:00
|
|
|
A = Main(dev)
|
|
|
|
B = render(dev)
|
|
|
|
print(":aaa")
|
|
|
|
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-01-11 23:21:48 +08:00
|
|
|
print("gogogogogogoogogogogo")
|
|
|
|
dev.a = False
|
|
|
|
print("A is deadddddddddddddddddddd AAAAAAAA")
|
|
|
|
time.sleep(3)
|
|
|
|
dev.b = False
|
|
|
|
print("BBBBBBBBBBBBBB")
|