비동기 호출과 리셋 메커니즘

6470 단어
#         
  #1,    :     ,           ,    ,        ,        
import time, random
from concurrent.futures import ThreadPoolExecutor
def la(name):
    print('%s    !'%name)
    time.sleep(random.randint(3,5))
    res = random.randint(7, 13) * '#'
    return {'name':name, 'res': res}

def weigh(shit):
    name = shit['name']
    size = len(shit['res'])
    print('%s  %skg '%(name, size))
if __name__ == '__main__':
    pool = ThreadPoolExecutor(13)
    shit1 = pool.submit(la, 'alex').result()
    print(shit1)
    weigh(shit1)
    shit2 = pool.submit(la, 'wupeqi').result()
    weigh(shit2)
    shit3 = pool.submit(la, 'yuanhao').result()
    weigh(shit3)


  #2,    

import time, random
from concurrent.futures import ThreadPoolExecutor
def la(name):
    print('%s    !'%name)
    time.sleep(random.randint(3,5))
    res = random.randint(7, 13) * '#'
    return {'name':name, 'res': res}

def weigh(shit):
    shit = shit.result()
    name = shit['name']
    size = len(shit['res'])
    print('%s  %skg '%(name, size))
if __name__ == '__main__':
    pool = ThreadPoolExecutor(13)
    shit1 = pool.submit(la, 'alex').add_done_callback(weigh)
    shit2 = pool.submit(la, 'wupeqi').add_done_callback(weigh)
    shit3 = pool.submit(la, 'yuanhao').add_done_callback(weigh)
#add_done_callback(  ),      

좋은 웹페이지 즐겨찾기