设为首页 加入收藏

TOP

Python学习——Python进程(一)
2017-10-09 13:49:37 】 浏览:2808
Tags:Python 学习 进程

  python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程。Python提供了非常好用的多进程包multiprocessing,只需要定义一个函数,Python会完成其他所有事情。借助这个包,可以轻松完成从单进程到并发执行的转换。multiprocessing支持子进程、通信和共享数据、执行不同形式的同步,提供了Process、Queue、Pipe、Lock等组件。

1、Process

创建进程的类:Process([group [, target [, name [, args [, kwargs]]]]]),target表示调用对象,args表示调用对象的位置参数元组。kwargs表示调用对象的字典。name为别名。group实质上不使用。

方法:is_alive()、join([timeout])、run()、start()、terminate()。其中,Process以start()启动某个进程。

属性:authkey、daemon(要通过start()设置)、exitcode(进程在运行时为None、如果为–N,表示被信号N结束)、name、pid。其中daemon是父进程终止后自动终止,且自己不能产生新进程,必须在start()之前设置。

注:

is_live()用来查看进程的状态

terminate()用来终止进程。

单进程:

 1 import multiprocessing
 2 import time
 3 def worker(interval):
 4     n=5
 5     while n > 0:
 6         print("The time is {0}".format(time.ctime()))
 7         time.sleep(interval)
 8         n -=1
 9 
10 if __name__ == "__main__":
11     p = multiprocessing.Process(target=worker,args=(3,))
12     p.start()
13     print("p.pid:",p.pid)
14     print("p.name:",p.name)
15     print("p.is_alive:",p.is_alive())

多进程:

 1 import multiprocessing
 2 import time
 3 
 4 def worker_1(interval):
 5     print ("worker_1")
 6     time.sleep(interval)
 7     print ("end worker_1")
 8 
 9 def worker_2(interval):
10     print ("worker_2")
11     time.sleep(interval)
12     print ("end worker_2")
13 
14 def worker_3(interval):
15     print ("worker_3")
16     time.sleep(interval)
17     print ("end worker_3")
18 
19 if __name__ == "__main__":
20     p1 = multiprocessing.Process(target = worker_1, args = (2,))
21     p2 = multiprocessing.Process(target = worker_2, args = (3,))
22     p3 = multiprocessing.Process(target = worker_3, args = (4,))
23 
24     p1.start()
25     p2.start()
26     p3.start()
27     # 用来获得当前的CPU的核数,可以用来设置接下来子进程的个数。
28     # 用来获得当前所有的子进程,包括daemon和非daemon子进程。
29     # p.name,p.pid分别表示进程的名字,进程id。 
30     print("The number of CPU is:" + str(multiprocessing.cpu_count()))
31     for p in multiprocessing.active_children():
32         print("child   p.name:" + p.name + "\tp.id" + str(p.pid))
33     print ("END!!!!!!!!!!!!!!!!!")

将进程定义为类:

 1 import multiprocessing
 2 import time
 3 
 4 class ClockProcess(multiprocessing.Process):
 5     def __init__(self, interval):
 6         multiprocessing.Process.__init__(self)
 7         self.interval = interval
 8 
 9     def run(self):
10         n = 5
11         while n > 0:
12             print("the time is {0}".format(time.ctime()))
13             time.sleep(self.interval)
14             n -= 1
15 
16 if __name__ == '__main__':
17     p = ClockProcess(3)
18     p.start()

daemon程序对比结果:
1.不加daemon

 1 import multiprocessing
 2 import time
 3 
 4 def worker(interval):
 5     print("work start:{0}".format(time.ctime()));
 6     time.sleep(interval)
 7     print("work end:{0}".format(time.ctime()));
 8 
 9 if __name__ == "__main__":
10     p = multiprocessing.Process(target = worker, args = (3,))
11     p.start()
12     print ("end!")
13 
14 #程序运行结果
15 '''
16 end!
17 work start:Wed Jun 28 00:07:57 2017
18 work end:Wed Jun 28 00:08:00 2017
19 '''

2.加daemon

 1 import multiprocessing
 2 import time
 3 
 4 def worker(interval):
 5     print("work start:{0}".format(time.ctime()));
 6     time.sleep(interval)
 7     print("work end:{0}".format(time.ctime()));
 8 
 9 if __name__ == "__main__":
10     p = m
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇ruby之gem install 下一篇我的Python开发之路---微信网页授..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目