#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import sys
sys.path.append('../..')
import task
import time

task_num = 0


def time_print(txt):
    sys.stdout.write(time.asctime() + ': ' + txt + '\n')


def hello(rt, name):
    global task_num
    task_num += 1
    if task_num >= 5:
        rt.stop()
    tn = task_num
    time_print("(Task %d) Hello %s!" % (tn, name))
    time.sleep(3.8)
    time_print("(Task %d) Ende!" % (tn))


print("task.queue example:\n----------------------")
q = task.queue()
q.enqueue(5, hello, "from queue example (5)")
q.enqueue(6, hello, "from queue example (6)")
q.enqueue(4, hello, "from queue example (4)")
q.run()
