Пример #1
0
def test_async_task():
    def async_process():
        num = 0
        for i in range(100):
            num += i
        return num

    # 1. 初始化一个线程池
    test_pool = pool.ThreadPool()
    test_pool.start()
    # 2. 生成一系列的任务
    for i in range(10):
        async_task = task.AsyncTask(func=async_process)
        test_pool.put(async_task)
        result = async_task.get_result()
        print('Get result: %d' % result)
Пример #2
0
def test_async_task():
    def async_process():
        num = 0
        for i in range(100):
            num += i
        return num

    # initalize a thread pool
    test_pool = pool.ThreadPool()
    test_pool.start()

    # generate tasks
    for i in range(10):
        async_task = task.AsyncTask(func=async_process)
        test_pool.put(async_task)
        result = async_task.get_result()
        print('Get result: %d' % result)
Пример #3
0
def test_async_task2():
    def async_process():
        num = 0
        for i in range(100):
            num += i
        time.sleep(5)
        return num

    # 1. 初始化一个线程池
    test_pool = pool.ThreadPool()
    test_pool.start()
    # 2. 生成一系列的任务
    for i in range(1):
        async_task = task.AsyncTask(func=async_process)
        test_pool.put(async_task)
        print('get result in timestamp: %d' % time.time())
        result = async_task.get_result()
        print('Get result in timestamp: %d: %d' % (time.time(), result))
Пример #4
0
def test_async_task2():
    def async_process():
        num = 0
        for i in range(100):
            num += i
        time.sleep(2)
        return num

    # initalize a thread pool
    test_pool = pool.ThreadPool()
    test_pool.start()

    # generate tasks
    for i in range(1):
        async_task = task.AsyncTask(func=async_process)
        test_pool.put(async_task)
        print('Get result in timestamp: %d' % time.time())
        result = async_task.get_result()
        print('Get result in timestamp: %d: %d' % (time.time(), result))