#!/usr/bin/env python3
# python 线程测试


import _thread
import time

from yvhai.demo.base import YHDemo


def print_time(thread_name, interval, times):
    for cnt in range(times):
        time.sleep(interval)
        print(" -- %s: %s" % (thread_name, time.ctime(time.time())))


class RawThreadDemo(YHDemo):
    def __init__(self):
        super(RawThreadDemo, self).__init__('_thread')

    @staticmethod
    def main():
        try:
            _thread.start_new_thread(print_time, ("Thread-01", 1, 10))
            _thread.start_new_thread(print_time, ("Thread-02", 2, 6))
        except:
            print("Error: 无法启动线程")

        # 主线程无限等待
        while 1:
            pass

    @staticmethod
    def demo(args=[]):
        RawThreadDemo.main()


if __name__ == '__main__':
    RawThreadDemo.demo()

[((13, 8, 13, 28), 'time.sleep', 'time.sleep', ({(13, 19, 13, 27): 'interval'}, {}), '(interval)', False, 'import time\n'), ((24, 12, 24, 70), '_thread.start_new_thread', '_thread.start_new_thread', ({(24, 37, 24, 47): 'print_time', (24, 49, 24, 69): "('Thread-01', 1, 10)"}, {}), "(print_time, ('Thread-01', 1, 10))", False, 'import _thread\n'), ((25, 12, 25, 69), '_thread.start_new_thread', '_thread.start_new_thread', ({(25, 37, 25, 47): 'print_time', (25, 49, 25, 68): "('Thread-02', 2, 6)"}, {}), "(print_time, ('Thread-02', 2, 6))", False, 'import _thread\n'), ((14, 54, 14, 65), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n')]