码迷,mamicode.com
首页 > 系统相关 > 详细

多进程模块:multiprocessing

时间:2019-02-08 01:23:51      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:time   span   get   假设   RoCE   通过   thread   threading   执行   

多进程:

(1) 前面我们学习的多线程,其实算不上真正的多线程,即使你开了很多个线程,在同一时间内只能有一个CPU核数来处理一个线程
(2) 在 python 中,多进程算得上是真正的多线程,假设你的CPU有四核,如果开四个子进程,四个CPU核数会同时处理这四个子进程
(3) 在 threading 中,我们是通过 threading.Thread(target=function, args=(....)) 来创建一个对象,然后再通过对象的 start() 方法来运行一个子线程,多个子线程可以并发执行
(4) 在 multiprocessing 中,我们是通过 multiprocessing.Process(target=function, args=(....)) 来创建一个对象,然后再通过对象的 start() 方法来运行一个子进程,多个子进程可以并发执行

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os
import time
import multiprocessing

def fun():
    print hello world, os.getpid(), os.getppid()
    time.sleep(1)

for i in range(10):
    p = multiprocessing.Process(target=fun, args=())
    p.start()
[root@localhost ~]$ python 1.py 
hello world 4056 4055
hello world 4057 4055
hello world 4058 4055
hello world 4059 4055
hello world 4060 4055
hello world 4061 4055
hello world 4062 4055
hello world 4063 4055
hello world 4064 4055
hello world 4065 4055

 

 

 

 

 

      

多进程模块:multiprocessing

标签:time   span   get   假设   RoCE   通过   thread   threading   执行   

原文地址:https://www.cnblogs.com/pzk7788/p/10355787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!