python Thread ThreadPoolExecutor,as_completed

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)
import threading,time
sem=threading.Semaphore(value=2)

class HtmlSpider(threading.Thread):
    def __init__(self,url,sem):
        super().__init__()
        self.url=url
        self.sem=sem
    def run(self):
        time.sleep(2)
        print('%s html spider run'%self.url)
        self.sem.release()
class UrlProducer(threading.Thread):
    def __init__(self,  sem):
        super().__init__()
        self.sem = sem
    def run(self):
        for i in range(10):
            self.sem.acquire()
            th=HtmlSpider('http://www.howhy.com/page/%d'%i,self.sem)
            th.start()
if __name__=='__main__':
    urlproducer=UrlProducer(sem)
    urlproducer.start()

  

  

posted @ 2023-12-04 18:23  howhy  阅读(90)  评论(0编辑  收藏  举报