Python3.7 多線程與多進程實例


1. 多進程

使用的是concurrent.future

from concurrent.futures import ThreadPoolExecutor, as_completed, ProcessPoolExecutor


THREAD_POOL = ThreadPoolExecutor(4)



# write為函數名,'打球為參數'
def thread_write():
    all_task = []
    for _ in range(4):
        all_task.append(THREAD_POOL.submit(write, '打球'))
    for future in as_completed(all_task):
        future.result()



def write(moive):
    time.sleep(3)
    print(moive)
    time.sleep(2)

 

2. 多進程

from concurrent.futures import ThreadPoolExecutor, as_completed, ProcessPoolExecutor


PROCESS_POOL =ProcessPoolExecutor(4)


# write為函數名,'打球為參數'
def process_write():
    all_task = []
    for _ in range(4):
        all_task.append(PROCESS_POOL.submit(write, '打球'))
    for future in as_completed(all_task):
        future.result()

def write(moive):
    time.sleep(3)
    print(moive)
    time.sleep(2)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM