Python多进程multiprocessing

python的多线程有点鸡肋了,所以最近学了些多进程的东西,记录下!!


multiprocessing.Process(self, group=None, target=None, name=None, args=(), kwargs={})

其方法有:

is_alive(self)
       Return whether process is alive


join(self, timeout=None)
       Wait until child process terminates

run(self)
      Method to be run in sub-process; can be overridden in sub-class

start(self)
     Start child process

terminate(self)
      Terminate process; sends SIGTERM signal or uses TerminateProcess()


多进程的使用方法跟多线程是类似的,先看个例子:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import multiprocessing
import time

def first_func(i):
    num = 3
    while num > 0:
        print "now at:",time.ctime()
        time.sleep(i)
        num -=1

if __name__ == "__main__":
    mp = multiprocessing.Process(target=first_func, args=(2,))
    mp.start()

    print "pid:",mp.pid
    print "name:",mp.name
    print "is_alive:",mp.is_alive()


输出结果为:

pid: 63398
name: Process-1
is_alive: True
now at: Mon Nov 28 16:22:42 2016
now at: Mon Nov 28 16:22:44 2016
now at: Mon Nov 28 16:22:46 2016



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值