# mpy - multitasking - using _thread (preemptive scheduling)
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2025-09-18  initial release

# see also: asyncio   (cooperative scheduling)

import	time
import	_thread		as		th

# ======================== Test1 ============================

def task1():
    while(True):
        print('!', end='')
        time.sleep(1)
    

def task2():
    while(True):
        print('*', end='')
        time.sleep(0.3)

# taks keep running when script terminates
def test1():
    th.start_new_thread(task1, ())
    th.start_new_thread(task2, ())
#    while True:
#        pass								# task #3
    print('done')
        

# ======================== Test2 ============================

active = True

def task1():
    while(active):
        print('!', end='')
        time.sleep(1)
    

def task2():
    while(active):
        print('*', end='')
        time.sleep(0.3)


# tasks terminates when script terminates
def test2():
    global active
    
    th.start_new_thread(task1, ())
    th.start_new_thread(task2, ())
    for i in range(5):
        time.sleep(2)
        print(i, end='')				# task #3
    active = False
    print('\ndone')

# ======================== Test3 ============================

class Main:
    
    def __init__(self):
        self.active = True
        th.start_new_thread(self.task1, ())
        th.start_new_thread(self.task2, ())
        for i in range(5):
            time.sleep(2)
            print(i, end='')				# task #3
        self.active = False
        print('\ndone')
        
        
    def task1(self):
        while(self.active):
            print('!', end='')
            time.sleep(1)
        

    def task2(self):
        while(self.active):
            print('*', end='')
            time.sleep(0.3)

# =========================== main ===============================

#test2()
Main()

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