!/usr/bin/env python
# -*- coding:utf-8 -*-
import multiprocessing
##定义一个process子进程对象
import time
def worker(jiange):
time.sleep(jiange)
print('hello,world.')
if __name__ == "__main__":
p = multiprocessing.Process(target=worker,args=(2,))
p.start()
print(p.is_alive())
p.join(timeout=3) #等待子进程执行完毕或者是超时执行后续内容,阻塞功能
print('end time')
print(p.name)
print(p.pid)
# ##target为传入的函数
# ##arg为函数的参数,格式必须为tuple格式
#
# #统计cpu的个数
# p = multiprocessing.cpu_count()
# ##列出存在的子进程
# m = multiprocessing.active_children()
#
# print(p)
# print(m)
!/usr/bin/env python
# -*- coding:utf-8 -*-
'''多进程的例子'''
import time
import multiprocessing
def worker(name,interval):
print('{0} start'.format(name) )
time.sleep(interval)
print('{0} end'.format(name))
if __name__ == "__main__":
print('main start')
print(multiprocessing.cpu_count())
print(multiprocessing.active_children())
p1 = multiprocessing.Process(target=worker, args=('worker1',5))
p2 = multiprocessing.Process(target=worker, args=('worker2',5))
p3 = multiprocessing.Process(target=worker, args=('worker3',5))
p1.start()
p2.start()
p3.start()
print(multiprocessing.cpu_count())
print(multiprocessing.active_children())
print('main end')