我在写一个玩具炸弹的代码,可以植入和拆除。但我有个问题。当时间达到零时,p1进程应该终止p2进程,但我得到了一个错误。我不明白,因为p2进程可以很好地停止p1进程。我的整个代码:
import RPi.GPIO as GPIO
from rpi_lcd import LCD
from time import sleep
import time
from multiprocessing import Process
# RPi setup
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Button setup
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Yellow
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Blue
# LCD setup
lcd = LCD()
# Keypad setup
length = 6
col = [19, 13, 6, 5]
row = [21, 20, 16, 26]
for j in range(4):
GPIO.setup(col[j], GPIO.OUT)
GPIO.output(col[j], 1)
for i in range(4):
GPIO.setup(row[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Password checker
def check_keypad(length):
col = [19, 13, 6, 5]
row = [21, 20, 16, 26]
matrix = [["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]]
result = ""
while True:
for j in range(4):
GPIO.output(col[j], 0)
for i in range(4):
if GPIO.input(row[i]) == 0:
time.sleep(0.02)
result = result + matrix[i][j]
print(result)
while GPIO.input(row[i]) == 0:
time.sleep(0.02)
GPIO.output(col[j], 1)
if len(result) >= length:
return result
# Start sequence
def starter():
global password
x = 0
lcd.text("Starting...", 1)
sleep(5)
lcd.clear()
lcd.text("Input a password", 1)
matrix = [["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]]
passwordmaker = ""
while x != 1:
lcd.text(passwordmaker, 2)
for j in range(4):
GPIO.output(col[j], 0)
for i in range(4):
if GPIO.input(row[i]) == 0:
time.sleep(0.02)
passwordmaker = passwordmaker + matrix[i][j]
# print(passwordmaker) # thingy
while GPIO.input(row[i]) == 0:
time.sleep(0.02)
GPIO.output(col[j], 1)
if len(passwordmaker) == 6:
lcd.text(passwordmaker, 2)
password = passwordmaker
print("Password - " + password)
x = 1
sleep(0.5)
lcd.clear()
lcd.text("Initiating", 1)
lcd.text("startup sequence", 2)
sleep(2)
lcd.clear()
sleep(0.5)
# Timer
def timer():
timeA = 41 # 40 + 1
while timeA != 0:
sleep(1)
timeA = timeA - 1
lcd.text(str(timeA), 1)
print(timeA)
p2.terminate()
lcd.clear()
lcd.text("Boom!", 1)
# Code
def code():
y1 = 3 # Amount of tries
y2 = 0
for y in range(3):
# Password from keypad
y1str = str(y1)
text = "( " + y1str + " / 3 )"
lcd.text(text, 2)
result = check_keypad(length)
y1 = y1 - 1
# Password check
if result == password:
y2 = 1
break
# Correct password
if y2 == 1:
p1.terminate()
lcd.clear()
lcd.text("Deactivated", 1)
sleep(10)
# Incorrect password
elif y1 == 0 & y2 == 0:
p1.terminate()
lcd.clear()
lcd.text("Boom!", 1)
sleep(10)
# Multiprocessing setup
p1 = Process(target=timer)
p2 = Process(target=code)
# Stuff
starter()
p1.start()
p2.start()
有问题的部分:
def timer():
timeA = 41 # 40 + 1
while timeA != 0:
sleep(1)
timeA = timeA - 1
lcd.text(str(timeA), 1)
print(timeA)
p2.terminate()
lcd.clear()
lcd.text("Boom!", 1)
我想要终止p2进程,但我一直收到这个错误:
Process Process-1:
Traceback (most recent call last):
File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
self.run()
File "/usr/lib/python3.7/multiprocessing/process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "/home/pi/home/pi/Desktop/pybomb/main.py", line 115, in timer
p2.terminate()
File "/usr/lib/python3.7/multiprocessing/process.py", line 124, in terminate
self._popen.terminate()
AttributeError: 'NoneType' object has no attribute 'terminate'
我不明白为什么我一直收到这个错误,尽管我可以在这里终止p1进程:
def code():
y1 = 3 # Amount of tries
y2 = 0
for y in range(3):
# Password from keypad
y1str = str(y1)
text = "( " + y1str + " / 3 )"
lcd.text(text, 2)
result = check_keypad(length)
y1 = y1 - 1
# Password check
if result == password:
y2 = 1
break
# Correct password
if y2 == 1:
p1.terminate()
lcd.clear()
lcd.text("Deactivated", 1)
sleep(10)
# Incorrect password
elif y1 == 0 & y2 == 0:
p1.terminate()
lcd.clear()
lcd.text("Boom!", 1)
sleep(10)
发布于 2021-05-11 22:43:18
您可以依靠信号来终止这两个进程。在幕后,Process.terminate()方法向目标进程传递一个SIGTERM信号。您需要一个队列或管道来将彼此的PID发送给进程。只有父(主)进程可以这样做。在两个进程之前在你的主代码中创建一个管道。
from multiprocessing import Process, Pipe
import signal
import os
# rest of the code
# Multiprocessing setup
conn1, conn2 = Pipe()
p1 = Process(target=timer, args=(conn1,))
p2 = Process(target=code, args=(conn2,))
# Stuff
starter()
p1.start()
p2.start()
conn1.send(p2.pid)
conn2.send(p1.pid)
现在编辑这两个函数,将管道作为参数
def timer(pipe):
p2_pid = pipe.recv()
#rest of the code
#instead of terminate use
os.kill(p2_pid, signal.SIGTERM)
def code(pipe):
p1_pid = pipe.recv()
#rest of the code
#instead of terminate use
os.kill(p1_pid, signal.SIGTERM)
https://stackoverflow.com/questions/67488541
复制相似问题