python3.x 多进程 & 多线程

from multiprocessing import Process  #多进程
from multiprocessing import Pool     #多进程池

if __name__ == '__main__':
    print('Parent process %s.' % os.getpid())
    p = Pool(2)
    p.apply_async(main, args=())#没有带入参,如有入参,结束地方末尾需要写一个逗号,否则会有问题
    p.apply_async(main2, args=())
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')

reference:

Python多进程之multiprocessing模块和进程池的实现

https://www.cnblogs.com/xiaobeibei26/p/6484849.html

廖雪峰 - 多进程

https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431927781401bb47ccf187b24c3b955157bb12c5882d000

multiprocessing.Process.terminate()结束子进程将导致子进程无法执行finally块,如何解决?

https://segmentfault.com/q/1010000005077517

官方手册

https://docs.python.org/3.5/library/multiprocessing.html#multiprocessing.Process

【python】详解multiprocessing多进程-process模块(一)

https://blog.csdn.net/brucewong0516/article/details/85776194

多进程 + 信号量

http://bbs.chinaunix.net/thread-4133313-1-1.html

子进程使用 sys.exit(1)后,程序卡住不动了。应该使用:raise Exception

http://bbs.chinaunix.net/thread-4079070-1-1.html

raise用法

https://www.runoob.com/python3/python3-errors-execptions.html

ip list 多进程范例程序

http://www.mamicode.com/info-detail-1292996.html

#test code 1
def main():
    try:
        print('6')
        raise ValueError('Exit !!!')
        print('2')
    except ValueError:
        print('3')
    
    
if __name__ == '__main__':
    print('1')
    main()    
    print('4')


#result,注意2没有打印出来,且程序没有中断
>>> ================================ RESTART ================================
>>> 
1
6
3
4
>>> 

#test code 2
def main():
    print('6')
    raise ValueError('Exit !!!')
    print('2')

    
    
if __name__ == '__main__':
    print('1')
    main()    
    print('4')
#result,注意程序中断了
>>> ================================ RESTART ================================
>>> 
1
6
Traceback (most recent call last):
  File "C:\CI_tools\v7cat\raise_test.py", line 11, in <module>
    main()
  File "C:\CI_tools\v7cat\raise_test.py", line 4, in main
    raise ValueError('Exit !!!')
ValueError: Exit !!!
>>> 

猜你喜欢

转载自blog.csdn.net/qqyuanhao163/article/details/89293953