python Thread ThreadPoolExecutor,as_completed

发布时间 2023-12-04 18:24:53作者: howhy
import threading
from concurrent.futures import ThreadPoolExecutor,as_completed,wait
import time
#
# def task(name):
#     print('task: %s'%name)
local_data=threading.local()
local_data.name='local__data'
class MyThread(threading.Thread):
    def __init__(self,event):
        super().__init__()
        self.event=event
    def run(self):
        print('thread %s init finish,ready to run'%self.name)
        self.event.wait()
        print("thread %s start execute...."%self.name)

class MyThread1(threading.Thread):
    def __init__(self,cond,name):
        threading.Thread.__init__(self,name=name)
        self.cond=cond
    def run(self):
        self.cond.acquire()
        print(self.getName()+': aaaaa')
        self.cond.notify()
        self.cond.wait()
        print(self.getName() + ': bbbb')
        self.cond.notify()
        self.cond.wait()
        print(self.getName() + ': ccc')
        self.cond.notify()
        self.cond.release()
class MyThread2(threading.Thread):
    def __init__(self,cond,name):
        threading.Thread.__init__(self,name=name)
        self.cond=cond
    def run(self):
        self.cond.acquire()
        self.cond.wait()
        print(self.getName()+': 11111')
        self.cond.notify()
        self.cond.wait()
        print(self.getName() + ': 22222')
        self.cond.notify()
        self.cond.wait()
        print(self.getName() + ': 333333')
        self.cond.release()

class MyThreadLocal(threading.Thread):
    def run(self):
        print('==value before',threading.currentThread(),local_data.__dict__)
        local_data.name=self.getName()
        print("==value after",threading.currentThread(),local_data.__dict__)

def get_html(times):
    time.sleep(times)
    print('get html wait %d'%times)
    return times
executor=ThreadPoolExecutor(max_workers=3)
if __name__=='__main__':
    # event = threading.Event()
    # threads=[MyThread(event) for i in range(10)]
    # event.clear()
    # for th in threads:
    #     th.start()
    #     th.daemon
    # event.set()


    # cond=threading.Condition()
    # th1=MyThread1(cond,'AAA')
    # th2 = MyThread2(cond, '6666')
    # th2.start()
    # th1.start()
    # th1.join()
    # th2.join()
    # print('talk end')

    # print("==main before", local_data.__dict__)
    # th1=MyThreadLocal()
    # th1.start()
    # th1.join()
    # th2=MyThreadLocal()
    # th2.start()
    # th2.join()
    # print("==main after", local_data.__dict__)

    # task1=executor.submit(get_html,1)
    # task2 = executor.submit(get_html, 2)
    # task3 = executor.submit(get_html, 3)
    # task4 = executor.submit(get_html, 4)

    # tasks=[executor.submit(get_html,tm) for tm in range(1,4)]
    # for tk in  as_completed(tasks):
    #     tkret=tk.result()
    #     print('task result %d'%tkret)
    
    tasks=executor.map(get_html,[1,4,5])
    for tk in  tasks:
        print('task result %d'%tk)