'Multithreading for a second while loop function with variables: communication between the two threads when the values change?
I have a question about multi threading in python that I am but unsure off and I need a second opinion off. Multithreading is till kind of new to me and considering I am still learning I need some answers from people with a some expertise than what I have. At first this seems like a stupid question
You see we have two scripts that are supposed to run kind of a sync both running in a loop. One script is for the relay setting of RaspBerry Pi 4 the other one is for the pin listening loop to then activate certain functions when certain Pin scenario's have been met.
Because we cannot exactly combine the relay script and the RPI.GPIO script because of some errors. We had to seperate them into the two different scripts. Originally, we just run them both in debug mode and it worked. **But considering the multi threading makes it possible to run the relay script and gpio script at once plus we need to kind of control the "state" of the relays at some point in certain scenarios this was the option that we chose.
Now this is the code for the main script that gets executed. The second script named Relay_Script gets also called here and needs to be executed in a second thread. Before the main While Loop happens. The relays have to turn on/off after all or nothing works.
The two variables that this script are to control two specific pins which will be set to False at default. But they can change based on some if statemens in the loop.
import datetime
import time
import mysql.connector
import socket
import pygame
import os
import atexit
import sys
import http.client
import RPi.GPIO as GPIO
from getmac import get_mac_address as gma
import requests
from gpiozero import MotionSensor
from pygame import mixer
from i2crelay import I2CRelayBoard
import Script_Relay as relay_check
import uuid
from threading import Thread, current_thread
from multiprocessing import Process, current_process
def main():
state_1 = False
state_2 = False
pygame.init()
relaythread = Thread(target = Script_Relay.relay_check.mainfunction,args = (state_1, state_2))
relaythread.start()
While True:
Begin_Time = BeginTimeSelect()
EndTime = EndTimeSelect()
operational_period = time_comparison(Begin_Time,EndTime)
operational_status_update(operational_period)
operational_status = OperationalSelection()
if operational_status == "Operational":
print("Start PIN listening")
if GPIO.input(Machine_input_1-pin) == 0:
state_1 = True
state 2 = False
elif GPIO.input(Machine_input_1-pin) == 1:
state_1 = False
state 2 = True
Other CODE goes here
elif operational_status == "Outside of time range":
print("BLOCK THE UNIT")
state_1 = False
state 2 = False
Next up we have the relay script one with a main kind of function to execute the script.
import time
import board
import busio
import digitalio
from adafruit_mcp230xx.mcp23008 import MCP23008
# Initialize the I2C bus:
i2c = busio.I2C(board.SCL, board.SDA)
# Create an instance the MCP23008 class
mcp = MCP23008(i2c) # MCP23008
# Now call the get_pin function to get an instance of a pin on the chip.
# This instance will act just like a digitalio.DigitalInOut class instance
# and has all the same properties and methods (except you can't set pull-down
# resistors, only pull-up!). For the MCP23008 you specify a pin number from 0
# to 7 for the GP0...GP7 pins. For the MCP23017 you specify a pin number from
# 0 to 15 for the GPIOA0...GPIOA7, GPIOB0...GPIOB7 pins (i.e. pin 12 is GPIOB4).
pin0 = mcp.get_pin(0)
pin1 = mcp.get_pin(1)
pin2 = mcp.get_pin(2)
pin3 = mcp.get_pin(3)
pin4 = mcp.get_pin(4)
pin5 = mcp.get_pin(5)
pin6 = mcp.get_pin(6)
pin7 = mcp.get_pin(7)
# Setup pin0 as an output that's at a high logic level.
pin0.switch_to_output(value=False)
pin1.switch_to_output(value=False)
pin2.switch_to_output(value=False)
pin3.switch_to_output(value=False)
pin4.switch_to_output(value=False)
pin5.switch_to_output(value=False)
pin6.switch_to_output(value=False)
pin7.switch_to_output(value=False)
# Setup pin1 as an input with a pull-up resistor enabled. Notice you can also
# use properties to change this state.
pin1.direction = digitalio.Direction.INPUT
pin1.pull = digitalio.Pull.UP
#pin2.direction = digitalio.Direction.INPUT
#pin2.pull = digitalio.Pull.UP
#pin3.direction = digitalio.Direction.INPUT
#pin3.pull = digitalio.Pull.UP
#pin4.direction = digitalio.Direction.INPUT
#pin4.pull = digitalio.Pull.UP
#pin5.direction = digitalio.Direction.INPUT
#pin5.pull = digitalio.Pull.UP
#pin6.direction = digitalio.Direction.INPUT
#pin6.pull = digitalio.Pull.UP
#pin7.direction = digitalio.Direction.INPUT
#pin7.pull = digitalio.Pull.UP
def mainfunction(pin_3 : bool, pin_4: bool):
while True:
pin0.switch_to_output(value=True)
pin1.switch_to_output(value=True)
pin2.switch_to_output(value=True)
pin3.switch_to_output(value=pin_3)
pin4.switch_to_output(value=pin_4)
pin5.switch_to_output(value=True)
pin6.switch_to_output(value=True)
pin7.switch_to_output(value=True)
#def pin0_functionswitch_to_output(setvalue: bool):
# pin0.switch_to_output(value = setvalue)
#def pin1_functionswitch_to_output(setvalue: bool):
# pin1.switch_to_output(value = setvalue)
#def pin2_functionswitch_to_output(setvalue: bool):
# pin2.switch_to_output(value = setvalue)
#def pin3_functionswitch_to_output(setvalue: bool):
# pin3.switch_to_output(value = setvalue)
#def pin4_functionswitch_to_output(setvalue: bool):
# pin4.switch_to_output(value = setvalue)
#def pin5_functionswitch_to_output(setvalue: bool):
# pin5.switch_to_output(value = setvalue)
#def pin6_functionswitch_to_output(setvalue: bool):
# pin6.switch_to_output(value = setvalue)
#def pin7_functionswitch_to_output(setvalue: bool):
# pin7.switch_to_output(value = setvalue)
# Now loop blinking the pin 0 output and reading the state of pin 1 input.
# pin0.switch_to_output(value=False)
# time.sleep(0.5)
# pin1.switch_to_output(value=False)
# time.sleep(0.5)
# pin2.switch_to_output(value=False)
# time.sleep(0.5)
# pin3.switch_to_output(value=False)
# time.sleep(5)
# pin4.switch_to_output(value=False)
# time.sleep(0.1)
# pin5.switch_to_output(value=False)
# time.sleep(0.5)
# pin6.switch_to_output(value=False)
# time.sleep(0.5)
# pin7.switch_to_output(value=False)
# time.sleep(5)
# Read pin 1 and print its state.
# print("Pin 1 is at a high level: {0}".format(pin1.value))
if __name__ == '__mainfunction__':
mainfunction()
Now this is the part I am kind of unsure about. Say the variable changes the thread depends on..does it also change when the thread is running? For example if State_1 becomes true, does the value also update to the running thread?
Solution 1:[1]
After some testing and finding out about some matter of this scenario MULTITHREADING and even MULTIPROCESSING is not option for us. We just cannot run the both scripts scripts at the same time in the same main script because of the state of the board being setup. There is a conflict one that sadly cannot be resolved by multithreading or multiprocessing.
If we run both scripts at the same time in debug mode it works fine but not as a parrelel process.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
