美文网首页
multiprocessing 多进程编程

multiprocessing 多进程编程

作者: 那个晓文 | 来源:发表于2020-04-30 10:42 被阅读0次
# import os
# import time
# #fork 只能用于Linux、unix中, windows中会出现子进程
# pid=os.fork()
# print('bobby')
#
# if pid==0:
#     print('子进程 {},父进程是:{}'.format(os.getpid(),os.getppid()))
# else:
#     print('我是父进程:{} .'.format(pid))
#
# time.sleep(2)


#多进程编程
from concurrent.futures import ProcessPoolExecutor
import multiprocessing
import time
def get_html(n):
    time.sleep(n)
    print('sub_progress success')
    return n

if __name__=="__main__":
    # progress=multiprocessing.Process(target=get_html,args=(2,))
    # progress.start()
    # print(progress.pid)
    # progress.join()
    # print('main progress end')

    #使用线程池
    pool=multiprocessing.Pool(multiprocessing.cpu_count())#获取取cpu个数,也可以指定个数
    # result=pool.apply_async(get_html,args=(3,))
    #
    #
    # pool.close()#不再接收新的任务进来
    # #等待所有任务完成
    # pool.join()
    # print(result.get())

    #imap #和线程池的map方法是一样的
    for result in pool.imap(get_html,[1,5,3]):
        print('{} sleep success'.format(result))

    #imap_unordered #谁先完成就先打印谁
    for result in pool.imap_unordered(get_html,[1,5,3]):
        print('{} sleep success'.format(result))


"""
PID和PPID:
1、PID(process ID):
PID是程序被操作系统加载到内存成为进程后动态分配的资源。
每次程序执行的时候,操作系统都会重新加载,PID在每次加载的时候都是不同的。
2、PPID(parent process ID):PPID是程序的父进程号。
3、PID和PPID都是非零的整数。
4、PID是唯一的,一个PID只标识一个进程。
5、一个进程创建的另一个新进程称为子进程。相反地,创建子进程的进程称为父进程。
6、对于一个普通的用户进程,它的父进程就是执行它的哪个Shell,对于Linux而言,Shell就是bash。
7、bash所在的目录:[negivup@localhost bin]$ cd /bin | ls bash


"""

相关文章

网友评论

      本文标题:multiprocessing 多进程编程

      本文链接:https://www.haomeiwen.com/subject/ogxzwhtx.html