Having problems with multiprocessing Python – Processes aren’t running

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật