I’m having problems trying to finish a university project and hopefully someone can help. Since the project is quite big and the only thing that isn’t working is the multiprocessing technique, I will only ask about that topic.
My project tries to simulate an IoT app with Python, where the user can choose different options from AWS (each option in a process) to execute different codes (I tried using threads, but the code inside each thread is very volumptuos and one of the threads collapses).
In order to help simulate my project, I have created a simpler project where it reads from AWS and, depending on the value sent (from 1 to 4), one of the four processes has to run and print something into the terminal. I have another script that works as a variable library, where I have all my variables saved (including var.process, which has a default value of -1).
from multiprocessing import Process
from awscrt import mqtt, io
from awsiot import mqtt_connection_builder
import json
import requests
import boto3
from typing import Tuple
from datetime import date
import variables1 as var
def Process_1():
while var.stop_thread == False:
if (var.process == 1):
print("Process 1 running...")
def Process_2():
while var.stop_thread == False:
if (var.process == 2):
print("Process 2 running...")
def Process_3():
while var.stop_thread == False:
if (var.process == 3):
print("Process 3 running...")
def Process_4():
while (var.stop_thread == False):
if (var.process == 4):
print("Process 4 running...")
#Crear los procesos
process1 = Process(target=Process_1)
process2 = Process(target=Process_2)
process3 = Process(target=Process_3)
process4 = Process(target=Process_4)
event_loop_group = io.EventLoopGroup(1)
host_resolver = io.DefaultHostResolver(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
def on_message_received(topic, payload, dup, qos, retain, **kwargs):
print("Message received")
message = str(payload).replace("b'", "").replace("'", "")
if (message == '1'):
var.process = 1
elif (message == '1'):
var.process = 2
elif (message == '1'):
var.process = 3
elif (message == '1'):
var.process = 4
######################################################################
################ FUNCTIONS FOR AWS CONFIGURATION... ##################
# Cannot post this part of the code since it has private credentials #
######################################################################
#Iniciar los hilos
process1.start()
process2.start()
process3.start()
process4.start()
while True:
try:
pass
except KeyboardInterrupt:
print("nDispositivo desconectado")
var.stop_thread = True
process1.join()
process2.join()
process3.join()
process4.join()
break
When I run this example project it doesn’t do anything. When I send a value from 1 to 4, it should theoretically print “Process XX runnning…”, but it doesn’t…
Any idea why this is? I understand it’s a very simple project and the tasks executed in the different processes could be run in threads, but it’s a test project that replicates the same problem I’m having in my real project (the processes don’t run when you select one from AWS).
Let you know my complete project would have 4 processes running and two interruptions (one from the subscription to AWS and another one from the notification of BLE).
Thanks!
I have tried using threads with my complete project but one of the threads doesn’t execute certain functions correctly. Therefore, I suppose it’s because of processing capacity and have tried using multiprocessing, although now I have a different problem (instead of not running correctly one of the threads, none of the processes run).
4