# mPy - demonstrating inter task communication
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-02-12  Initial release

import	time

# ==================== Global Variable ========================

if 0:
    
    var_task1 = None


    def task1():
        global var_task1
        
        if var_task1:
            print(var_task1)
            var_task1 = None
            
            
    def task2():
        global var_task1
        
        var_task1 = 'Hello'
        
        
    def main():
        while True:
            task1()
            task2()					# 3635 cpu cycles (1)
            time.sleep(1)
        

    main()

# =================== Module Global Class =========================

if 0:
    
    class G:
        var_task1 = None


    def task1():
        if G.var_task1:
            print(G.var_task1)
            G.var_task1 = None
            
            
    def task2():
        G.var_task1 = 'Hello'
        
        
    def main():
        while True:
            task1()
            task2()					# 7456 cpu cycles (3)  +105%
            time.sleep(1)
        

    main()

# ====================== Application Class ===========================

if 0:
    
    class Application:
        
        def __init__(self):
            self.var_task1 = None
            self.main()
            

        def task1(self):
            if self.var_task1:
                print(self.var_task1)
                self.var_task1 = None
            
            
        def task2(self):
            self.var_task1 = 'Hello'
        
        
        def main(self):
            while True:
                self.task1()
                self.task2()	# 4481 cpu cycles (2)  +23%
                time.sleep(1)
        

    Application()

# ====================== Application Object ===========================

if 1:
    
    class Application:
        var_task1 = None
            
        @staticmethod
        def task1():
            if Application.var_task1:
                print(Application.var_task1)
                Application.var_task1 = None
            
            
        @ staticmethod
        def task2():
            Application.var_task1 = 'Hello'
        
        
        @ staticmethod
        def main():
            while True:
                Application.task1()
                Application.task2()		# 7547 cpu cycles (4)  +108%
                time.sleep(1)
        

    Application.main()

# ============================== end =========================
