Python自定义进程

cat custommulti.py 
#!/usr/bin/env python3
#coding:utf8
from multiprocessing import Process
import sys

class MyProcess(Process):
   def __init__(self,name):
      super(MyProcess,self).__init__()
      self.name=name
   #重写run方法
   def run(self):
      n=1
      while True:
         #print('进程名字:'+self.name)
         n+=1
         print('自定义进程:{},n的值:{}'.format(self.name,n))
         if n == 10:
            #sys.exit()
            break

if __name__ == '__main__':
   p1=MyProcess('进程1')
   p2=MyProcess('进程2')
   p1.start()
   p2.start()
[root@ceph01 python]# python3 custommulti.py 
自定义进程:进程1,n的值:2
自定义进程:进程1,n的值:3
自定义进程:进程1,n的值:4
自定义进程:进程1,n的值:5
自定义进程:进程1,n的值:6
自定义进程:进程1,n的值:7
自定义进程:进程1,n的值:8
自定义进程:进程2,n的值:2
自定义进程:进程2,n的值:3
自定义进程:进程1,n的值:9
自定义进程:进程1,n的值:10
自定义进程:进程2,n的值:4
自定义进程:进程2,n的值:5
自定义进程:进程2,n的值:6
自定义进程:进程2,n的值:7
自定义进程:进程2,n的值:8
自定义进程:进程2,n的值:9
自定义进程:进程2,n的值:10

猜你喜欢

转载自blog.csdn.net/weixin_40548182/article/details/112367189