• 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()
    
  • 相关阅读:
    相遇相知都是缘
    [BTS]6912,5641,5773,5410错误处理!
    [BTS]Cumulative Functloids用法
    职业生涯又一个转折点
    BizTalk Group Day 北京!
    [BTS]10008错误如何处理?
    [ERROR]创建BAM视图时报错
    [BTS]使用BizTalk开发应用系统,就是这么简单!
    [BTS]BizTalk2006 SqlAdapter UpdateGram的Update用法
    [BTS]为什么BTSharePointAdapterWS.asmx无法预览?
  • 原文地址:https://www.cnblogs.com/hankleo/p/10820402.html
Copyright © 2020-2023  润新知