zoukankan      html  css  js  c++  java
  • 20.multi_case03

    # 多线程
    import threading
    import time
    
    class myThread(threading.Thread):
        def __init__(self, threadID, name, counter):
            threading.Thread.__init__(self)
            self.threadID = threadID
            self.name = name
            self.counter = counter
    
        def run(self):
            print("Starting " + self.name)
            # 获得锁,成功获得锁定后返回True
            # 可选的timeout参数不填时将一直阻塞直到获得锁定
            # 否则超时后将返回False
            threadLock.acquire()
            print_time(self.name, self.counter, 3)
            # 释放锁
            threadLock.release()
    
    def print_time(threadName, delay, counter):
        while counter:
            time.sleep(delay)
            print("%s: %s" % (threadName, time.ctime(time.time())))
            counter -= 1
    
    threadLock = threading.Lock()
    threads = []
    
    # 创建新线程
    thread1 = myThread(1, "Thread-1", 1)
    thread2 = myThread(2, "Thread-2", 2)
    
    # 开启新线程
    thread1.start()
    thread2.start()
    
    # 添加线程到线程列表
    threads.append(thread1)
    threads.append(thread2)
    
    # 等待所有线程完成
    for t in threads:
        t.join()
    
    print("Exiting Main Thread")
    
    import threadpool
    import time
    
    def sayhello(a):
        print("hello: " + a)
        time.sleep(2)
    
    def main():
        global result
        seed = ["a", "b", "c"]
        start = time.time()
        task_pool = threadpool.ThreadPool(5)
        requests = threadpool.makeRequests(sayhello, seed)
        for req in requests:
            task_pool.putRequest(req)
        task_pool.wait()
        end = time.time()
        time_m = end - start
        print("time: " + str(time_m))
        start1 = time.time()
        for each in seed:
            sayhello(each)
        end1 = time.time()
        print("time1: " + str(end1 - start1))
    
    if __name__ == '__main__':
        main()
    
    from concurrent.futures import ThreadPoolExecutor
    import time
    
    def sayhello(a):
        print("hello: " + a)
        time.sleep(2)
    
    def main():
        seed = ["a", "b", "c"]
        start1 = time.time()
        for each in seed:
            sayhello(each)
        end1 = time.time()
        print("time1: " + str(end1 - start1))
        start2 = time.time()
        with ThreadPoolExecutor(3) as executor:
            for each in seed:
                executor.submit(sayhello, each)
        end2 = time.time()
        print("time2: " + str(end2 - start2))
        start3 = time.time()
        with ThreadPoolExecutor(3) as executor1:
            executor1.map(sayhello, seed)
        end3 = time.time()
        print("time3: " + str(end3 - start3))
    
    if __name__ == '__main__':
        main()
    
  • 相关阅读:
    vue实例讲解之axios的使用
    实例讲解webpack的基本使用第四篇
    实例讲解webpack的基本使用第三篇
    实例讲解webpack的基本使用第二篇
    写好一篇技术博客的正确姿势是什么
    实例讲解js正则表达式的使用
    一个综合实例讲解vue的基础知识点。
    vue实例讲解之vue-router的使用
    .NET 串口通信
    textarea赋值时换行符无效的解决方法
  • 原文地址:https://www.cnblogs.com/hankleo/p/10820402.html
Copyright © 2011-2022 走看看