# cPy - demonstrating pre-emptive multitasking
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 010  2025-10-06  initial release

import	time
from	threading	import	Thread

# ============================== demo ===============================

terminated = False


# print '@' @ 1 Sec
def task1():
    try:
        while not terminated:
            print('@', end='')
            time.sleep(1)
    finally: print('task1 terminated')


# print '*' @ 0.5 Sec
def task2():
    try:
        while not terminated:
            print('*', end='')
            time.sleep(0.5)
    finally: print('task2 terminated')


# print '.' @ 0.25 Sec
def task3():
    try:
        while not terminated:
            print('.', end='')
            time.sleep(0.25)
    finally: print('task3 terminated')


# main loop executing task1, task2 and task3
def main():
    global	terminated
    
    t1 = Thread(target=task1, args=()); t1.start()
    t2 = Thread(target=task2, args=()); t2.start()
    t3 = Thread(target=task3, args=()); t3.start()
    time.sleep(10)
    terminated = True; print()
    t1.join(); t2.join(); t3.join()
    print('main terminated')


main()

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