zoukankan      html  css  js  c++  java
  • Python的threading和multiprocessing

    Python的threading

    基础用法, 通过 threading.Thread() 创建线程, 然后 start() 和 join()

    import time
    import threading
    
    def do_something(seconds):
    	print('Sleeping...')
    	time.sleep(seconds)
    	print('Done')
    
    start = time.perf_counter()
    threads = []
    
    for _ in range(10):
    	t = threading.Thread(target = do_something, args=[1])
    	t.start()
    	threads.append(t)
    
    for t in threads:
    	t.join()
    
    finish = time.perf_counter()
    print('Total: {}'.format(round(finish - start, 2)))
    

    使用线程池. 使用as_completed, 可以阻塞并按完成顺序输出结果, 而直接用executor.map()会将结果收集完成后一起返回.

    import time
    import threading
    from concurrent import futures
    
    def do_something(seconds):
    	print('Sleeping...')
    	time.sleep(seconds)
    	return 'Done ' + str(seconds)
    
    start = time.perf_counter()
    with futures.ThreadPoolExecutor(max_workers=3) as executor:
    	secs = [3, 2.5, 2, 2.2, 0.5]
    	results = [executor.submit(do_something, sec) for sec in secs]
    	for f in futures.as_completed(results):
    		print(f.result())
    
    # 注意区别
    with futures.ThreadPoolExecutor() as executor:
    	secs = [3, 2.5, 2, 2.2, 0.5]
    	# 下面这行实际是阻塞的
    	results = executor.map(do_something, secs)
    	for result in results:
    		print(result)
    
    finish = time.perf_counter()
    print('Total: {}'.format(round(finish - start, 2)))
    

    .

    Python的multiprocessing

    .在使用multiprocessing时, 子进程里的print()是会滞后打印的.

    import time
    import multiprocessing
    import logging
    
    def do_something(seconds):
    	print('Sleeping...', seconds)
    	time.sleep(seconds)
    	return 'Done ' + str(seconds)
    
    if __name__ == '__main__':
    	multiprocessing.log_to_stderr()
    	logger = multiprocessing.get_logger()
    	logger.setLevel(logging.INFO)
    	start = time.perf_counter()
    	secs = [3.1, 3.5, 3.1, 3.2, 3.5, 3.3]
    	processes = []
    	for sec in secs:
    		p = multiprocessing.Process(target=do_something, args=(sec,))
    		p.start()
    		processes.append(p)
    
    	for p in processes:
    		p.join()
    
    	finish = time.perf_counter()
    	print('Total: {}'.format(round(finish - start, 2)))
    	print()
    
    	pool = multiprocessing.Pool(processes=3)
    	print(pool.map(do_something, secs))
    	finish = time.perf_counter()
    	print('Total: {}'.format(round(finish - start, 2)))
    

    .

  • 相关阅读:
    IDEA忽略某些文件
    Mac操作:Mac系统移动鼠标显示桌面(移动鼠标到角落)
    Mac流程图的软件
    Mac 电脑无法登陆 账号了
    学习如何管理-录视频
    项目倒入maven 遇到的问题只有 main 了
    下载代码的时候 SSH与http的区别
    Mac修改hosts方法
    MAC版本的UltraEdit破解方法
    intelliJ 社区版-找不到 plugins选项
  • 原文地址:https://www.cnblogs.com/milton/p/11733851.html
Copyright © 2011-2022 走看看