Monday, November 15, 2021

[SOLVED] AttributeError: 'NoneType' object has no attribute 'terminate'

Issue

I'm writing code for a toy bomb, that can be planted and defused. But I have a problem. When the time reaches zero the p1 process should terminate the p2 process, but i get an error. I don't get it since p2 process can stop the p1 process just fine. My whole code:

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()

The problematic bit :

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)

I want to terminate p2 process, but i keep getting this error :

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'

I don't get it why i keep getting that error even tho, I can terminate p1 process here:

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)

Solution

You can rely on signals to terminate both processes. Under the hood, the Process.terminate() method delivers a SIGTERM signal to the target process. You need a Queue or Pipe to sent to the processes each other PID. Only the parent (main) process can do so. In your main code before the two processes create a pipe.

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)

Now edit both your functions to take the pipe as an argument

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)


Answered By - kinshukdua